Error using textscan Mismatch between file and format character vector. Trouble reading 'Numeric' field from file (row number 1, field number 3)

12 views (last 30 days)
I get the following error:
Error in Ti2_Auto_9_20x_USE_addedcode (line 11)
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError',
false);
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 3) ==>
t1xy10z12.tif,33,255,255,9.91277508,4.23866220,34.77334951,0.59568542,100,2.33865654,0.42759592,0.74157303\n
From this code:
for i = 1:length(list)-2
filename = list(i+2,1).name;
delimiter = ',';
startRow = 2;
formatSpec = '%f%f%f%f%f%f%f%f%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
tem = [];
for n = 1:12
tem(:,n) = dataArray{1,n};
end
Here is the first .csv file being read/errored on (also attached):
Any help to resolve this is appreciated!

Answers (1)

Stephen23
Stephen23 on 11 Feb 2020
Edited: Stephen23 on 12 Feb 2020
The second field is clearly not numeric, containing values like
020520t1xy10z12.tif
020520t1xy10z12.tif
020520t1xy10z12.tif
020520t1xy10z12.tif
020520t1xy10z12.tif
020520t1xy10z12.tif
etc.
so you will need to import it as character, eg:
formatSpec = '%f%s%f%f%f%f%f%f%f%f%f%f%[^\n\r]';
% ^^ second field as character
Tip: simply use the CollectOutputs option rather than that inefficient loop at the end of your code.

Community Treasure Hunt

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

Start Hunting!