Index exceeds the number of array elements (1)

1 vue (au cours des 30 derniers jours)
Abby Olaleye
Abby Olaleye le 15 Oct 2020
Réponse apportée : KSSV le 15 Oct 2020
I'm confused as to why I'm getting this error:
Index exceeds the number of array elements (1).
Error in ReadVTData2 (line 25)
col2(i) = str2double(str_col(2));
Error in ProcessVT (line 8)
[data, numrows]= ReadVTData2(VTfilename);
It works well for line 24 but not 25. Please explain.

Réponse acceptée

KSSV
KSSV le 15 Oct 2020
Your file nums2.txt has alternate lines of data..every alterate line is a empty line, when you try to read using the file using line:
getrow=fgetl(fileID);
you will get empty str_col
So these lines:
col1(i) = str2double(str_col(1));
col2(i) = str2double(str_col(2));
throwing error.
Two options:
  1. Remove the alternate empty lines
Or, use if else conditon in the file ReadVTData2.m
if length(str_col) > 2
col1(i) = str2double(str_col(1));
col2(i) = str2double(str_col(2));
end
NOTE: There are many other elegant ways of reading such files. You can better use readtable.

Plus de réponses (0)

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by