Trouble Reading Multiple .csv Files. "Intermediate dot '.' indexing produced a comma-separated list..." Error.

29 views (last 30 days)
Amanda Fawley
Amanda Fawley on 22 Oct 2021
Edited: Stephen on 23 Oct 2021
Hello. I am trying to get Matlab to take 5 columns of data from multiple (6) .csv files, put the raw data in a structure, then read the raw data and seperate each column into it's own column in the same structure. When my 'for' loop tries to read the RawData to put it into seperate it into individual columns in the structure, I get a "Intermediate dot '.' indexing produced a comma-separated list with 2 values, but it must produce a single value to perform subsequent indexing operations." message.
1) What does this mean? I have searched what it is, but it's not clear to me.
2) Given my code (see below), does anyone know how to fix this issue?
Notes/Context: My first 4 rows of my csv files are headers (hence why I tell it to start reading data at row 5). Also, the length of data in each file is inconsistent (hence the 'ending' variable to read the hieght of the raw data in each csv).
Any advice would be greatly appreciated. Thank you for your time.
for i = 1:6
filename = uigetfile; %Select file
DataID(i).RawData = dlmread(filename,',', 4, 0); %Store raw data under "RawData" header in structure
ending = height(DataID(i).RawData); %Determine hieght of data
DataID(i).AxialStrain = DataID.RawData(1:ending,1); %Read first column of RawData and store data under 'AxialStrain' header in same structure
DataID(i).VolStrain = DataID.RawData(1:ending,2); %Read second column of RawData and store data under 'VolStrain' header in same structure
DataID(i).DeltaU = DataID.RawData(1:ending,3); %Read third column of RawData and store data under 'AxialStrain' header in same structure
DataID(i).p = DataID.RawData(1:ending,4); %Read fourth column of RawData and store data under 'AxialStrain' header in same structure
DataID(i).q = DataID.RawData(1:ending,5); %Read fifth column of RawData and store data under 'AxialStrain' header in same structure
end

Accepted Answer

Stephen
Stephen on 22 Oct 2021
Edited: Stephen on 23 Oct 2021
DataID(i).AxialStrain = DataID(i).RawData(1:ending,1);
% ^^^ you need this index every time you refer to DataID.
What is happening is that DataID.RawData refers to the RawData field of every file that you have imported, so it will create a comma-separated list just as if you wrote this (for every element of DataID):
DataID(1).RawData, DataID(2).RawData, .. , DataID(end).RawData
But what you want is to refer only the the i-th RawData field (for the i-th file), so you need to use the indexing to select the i-th element of the DataID structure array, as I showed above.
Personally I would simplify the code by doing something like this:
for k = 1:6
..
M = dlmread(filename,',', 4, 0);
DataID(i).RawData = M;
DataID(i).AxialStrain = M(:,1);
DataID(i).VolStrain = M(:,2);
DataID(i).DeltaU = M(:,3);
DataID(i).p = M(:,4);
DataID(i).q = M(:,5);
end
and calling UIGETFILE just once before the loop with 'MultiSelect' set to 'on' (much more user-friendly).

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!