textscan error: Mismatch between file and format character vector - Trouble reading 'Numeric' field from file
5 次查看(过去 30 天)
显示 更早的评论
How do we resolve this error?
Error using textscan
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
"0.0","100.0","100.0","100.0","100.0","100.0","100.0"\n
Error in importeditedMIMDVH (line 43)
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue'
,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
I used the Import Data tool to generate a function to run in a loop to import data from multiple CSV files.
The pertinent code:
delimiter = ',';
if nargin<=2
startRow = 3;
endRow = inf;
end
formatSpec = '%f%f%f%f%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
The CSV file's opening lines (first line has changed letters), from beginning to what I think is the line triggering the error:
,"ABCDE FGHIJ, KLMNO",,,,,
,prostate (8)(Volume: 39.71),Proximal-SV (12)(Volume: 6.06),rectum (9)(Volume: 77.69),bladder (10)(Volume: 169.40),PTV1 (19)(Volume: 116.46),PTV2 (21)(Volume: 96.08)
0,100,100,100,100,100,100
This function is accessed by the last line of this pertinent code:
files = dir('C:\Users\Myname\Documents\data\DVH\full\*.csv');
numberoffiles = length(files);
for loop = 1:numberoffiles
if isequal(loop,2) || isequal(loop,8)
[dose,prostate,ProximalSV,rectum,bladder,PTV1,PTV2] = importeditedMIMDVH([files(loop).folder,'\',files(loop).name]);
I have tried auto-generating the function a second time (the same code was generated); removing \n\r and \r from formatSpec; using single %f32 instead of double; and adding ' ' to [^...] in formatSpec. Same error message. The error occurs on both files 2.csv and 8.csv, the two files I had to edit manually to rearrange the data columns in Excel 2016, which stripped the " quotation marks around the data. The other auto-generated function for the unedited files works, which uses this formatSpec instead, without problem:
formatSpec = '%q%q%q%q%q%q%q%[^\n\r]';
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
The only suspicious difference I see between the auto-generated import function that works and the one that doesn't, and the data sets, is that for the problematic data files, every quotation mark is removed except the ones around the character string in the first line. Are those sole remaining quotation marks -- whereas Excel strips every other for all other CSV in the file -- indicative of Excel 2016 changing the file to trigger a MATLAB bug? I deleted the quotation marks and tried again but the same error message occurs.
1 个评论
Stephen23
2017-3-13
All the data files, regardless of whether quotation marks enclosed data, were able to be imported with
formatSpec = '%q%q%q%q%q%q%q%[^\n\r]';
whereas the error occurs with specific files when trying to use
formatSpec = '%f%f%f%f%f%f%f%[^\n\r]';
even though this code is auto-generated by MATLAB.
What is the cause of this error, though?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!