please help with indexing error
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
John BG
el 8 de Feb. de 2016
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
Respuestas (1)
Walter Roberson
el 8 de Feb. de 2016
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 comentarios
Steven Lord
el 8 de Feb. de 2016
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.
Ver también
Categorías
Más información sobre String en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!