please help with indexing error

1 次查看(过去 30 天)
I wrote this code to look into an array of cells with filenames, then open said files, extract data, and store it in the array named "Data". the first file that satisfies the if statement is n=206. the for loop functions properly for this one, but when n=207, I get the following error,
??? Index exceeds matrix dimensions. Error in ==> DataProc at 42 Data(n,:)=[CompYoN(1),CompYoN(2),CompYoN(3),MaxDf(1),MaxDf(2),MaxDf(3),MinDf(1),...
the "Data" array has over 400 rows, so why is the indexing error coming up when I try to acssess row 207?
Code
for n=1:1:length(FileNames)
F =char(FileNames(n))
fileID = fopen(F,'r')
if fileID == -1
else
C = textscan(fileID,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%s');
fclose(fileID);
MaxF=C{1};,MinF=C{2};,LDT=C{3};,MaxDf=C{4};,MinDf=C{5};,Fdef=C{6};,MaxFTol=C{7};,MinFTol=C{8};
MaxDef=C{9};,MinDef=C{10};,MaxDfLim=C{11};,MinDfLim=C{12};,CompYoN=C{13};
Data(n,:)=[CompYoN(1),CompYoN(2),CompYoN(3),MaxDf(1),MaxDf(2),MaxDf(3),MinDf(1),...
MinDf(2),MinDf(3),Fdef(1),Fdef(2),Fdef(3),MaxF(1),MaxF(2),MaxF(3),MinF(1),MinF(2),MinF(3),...
LDT(1),LDT(2),LDT(3)]
clear C
end
end
  2 个评论
John BG
John BG 2016-2-8
without the data file, I am sure you understand it's a bit of guesswork to attempt any help. Could you please hand the data file? awaiting answer
John
Steven Reuter
Steven Reuter 2016-2-8
255.112986,252.006048,5.004000,2.343598,2.277321,0.303717,260.000000,250.000000,3.100000,2.160000,0.810000,0.200000,Completed
255.119929,252.954809,5.004000,2.351693,2.270392,0.319233,260.000000,250.000000,3.100000,2.160000,0.810000,0.200000,Completed
255.165115,253.260640,5.004000,2.366201,2.299397,0.319692,0.000000,-5.000000,10.000000,-1.000000,0.000000,0.000000,Completed

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2016-2-8
The problem is not indexing Data, the problem is indexing one of the variables you extract from the cell array C. If fewer than 3 lines were read by the textscan then indexing the third of the values would not work.
Also, by my count CompYoN=C{13} is a cell array of strings, so when you have
[CompYoN(1),CompYoN(2),CompYoN(3), MaxDf(1)...]
the first three of those are cell array entries and the rest are numeric. The result is going to be a cell array in which each string and each numeric entry is in a different cell. You are then assigning that cell array into data(n,:) . If that is the behaviour you want, then I recommend that you make it easier for other people to read by coding
Data(n,:) = {CompYoN{1}, CompYoN{2}, CompYoN{3}, MaxDf(1), MaxDf(2), MaxDf(3), MinDf(1), ...
MinDf(2), MinDf(3), Fdef(1), Fdef(2), Fdef(3), MaxF(1), MaxF(2), MaxF(3), MinF(1), MinF(2),MinF(3), ...
LDT(1), LDT(2), LDT(3)};
This would make clear that you know that you are working with cell inputs for the CompYoN field, and that you know you want a cell output.
  3 个评论
Steven Lord
Steven Lord 2016-2-8
There's probably something different about your 207th file. Open the file whose name is stored in FileNames(207) and compare its format to that of the file whose name is stored in FileNames(206). I bet you've got something extra or something missing in the first of those files.
Steven Reuter
Steven Reuter 2016-2-9
So the files are the same, but when the for loop runs the second time, the formatting that it uses in textscan changes, first it takes the numberes as [double] and the string as a cell, then it changes the format on the second itteration
F =
Sum-1A3112BG4000-8794.dat
fileID =
3
C =
Columns 1 through 12
[3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double] [3x1 double]
Column 13
{3x1 cell}
F =
Sum-1A3112BG4001-8795.dat
fileID =
3
C =
[255.0747] [252.1103] [5.0040] [2.6327] [2.5585] [0.3787] [260] [250] [3.1000] [2.1600] [0.8100] [0.2000] {1x1 cell}
??? Index exceeds matrix dimensions.
Error in ==> DataProc at 42 Data(n,:)=[CompYoN(1),CompYoN(2),CompYoN(3),MaxDf(1),MaxDf(2),MaxDf(3),MinDf(1),...
>>

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by