Index exceeds number of array elements
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
Please excuse my naivety as a beginner with MATLAB but I am receiving an error message I'm struggling to get past. I am ultimately trying to plot a dataset.
for i=1:nVal
[fline,count] = fscanf (fid,'%f %f %f %f %f',5);
if (mod(i,20) == 1)
fprintf ('%6.1f %7.2f\n', fline(1), fline(2) ); % display the time and x value as a check
end
t(i) = fline(1); % load vector of time values
xm(i) = fline(2); % load vector of x readings
ym(i) = fline(3); % load vector of y readings
z1m(i) = fline(4); % load vector of z1 readings
z2m(i) = fline(5); % load vector of z2 readings
end
The error message that is coming up is:
Index exceeds the number of array elements. Index must not exceed 0.
Error in untitled (line 40)
fprintf ('%6.1f %7.2f\n', fline(1), fline(2) ); % display the time and x value as a check
There is of course more code before and after this and as a noobie, I wasn't sure how much would have been required to help solve this solution but this is the section that appears to be causing the error. Any help is much appreciated!
0 Kommentare
Akzeptierte Antwort
Torsten
am 19 Aug. 2022
Insert
class(fline)
size(fline)
after
for i=1:nVal
[fline,count] = fscanf (fid,'%f %f %f %f %f',5);
and see what type and size the variable "fline" has that you try to output in the following command
if (mod(i,20) == 1)
fprintf ('%6.1f %7.2f\n', fline(1), fline(2) ); % display the time and x value as a check
end
5 Kommentare
Walter Roberson
am 19 Aug. 2022
Is that within the
for i=1:nVal
loop? Because if it is, then i cannot be 0 there.
What the output is showing you is that your input is empty on that particular line.
Furthermore, fscanf() with a numeric % format will skip all leading white space.
- you might have reached end of file
- there might be several blank lines, followed by something that is not blank but starts with something that is not numeric.
- You might have accidentally opened the file with 'w' or 'w+' or 'a' permission instead of 'r' or 'r+' or 'a+' permission. 'w' and 'w+' would start by erasing everything in the file. 'a' (but not 'a+') would automatically position at the end of file so there might not be anything in position to read.
- Are you looping over files? Because if so then you might have forgotten to close a previous file and open a new one
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!