Matlab failing to import data
Afficher commentaires plus anciens
I've written a script that imports all .csv files from the current folder so as to plot the data. This works absolutely fine with my home computer which runs R2013a. However, my work computer (version R2015a) fails to import the files beyond the first row of data and I get the following error:
Error using dlmread (line 138)
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 2, field number 1) ==>
4.000000E-8,1.238711E-2\n
I don't believe that this is an issue with my script as Matlab also fails to read the file beyond the first 9 lines if I import the file using the 'Import Data' button (the script works fine on my home computer, after all). I've tried reinstalling my work Matlab R2015a, but the problem persists.
The files have 8 lines of blurb and the numerical values begin on line 9. They look like this:
TOF Data File
30/04/2015
10:26
Bias -- 0.000V
Thickness -- 1.00000um
Q/Q0 -- Inf
Mobility -- 0.00000E+0cm2/V.s
Time (s) Voltage (V)
0.00E+00 1.22E-04
8.00E-10 1.06E-03
1.60E-09 -3.32E-03
2.40E-09 -1.44E-03
3.20E-09 5.59E-03
4.00E-09 6.68E-03
4.80E-09 -2.85E-03
etc...
Only row 9 is imported. What could this issue be? I've attached screen shots of how the data is imported in Matlab.
Thanks!
3 commentaires
Ingrid
le 26 Mai 2015
no files attached.
are you sure this is a problem of matlab and not of different regional settings on the two computers (think of . and , to denote decimals)
Jorge Costa
le 26 Mai 2015
Jorge Costa
le 27 Mai 2015
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 26 Mai 2015
fid = fopen('13V.csv', 'rt'); %the 't' is important!
C = textscan('%f,%f','HeaderLines',8);
fclose(fid);
Time = C{1};
Voltage = C{2};
Opening the file in text mode turned out to be important. An alternate solution would be to open as normal, fopen('13V.csv'), but then to specify 'LineTerminator', '\r\n' in the call to textscan()
4 commentaires
Jorge Costa
le 27 Mai 2015
Jeremy Hughes
le 27 Mai 2015
Hi Jorge,
Walter's code looks close, but there are a couple of things missing that might create some problems. Without looking directly at your file, there might be additional problems to work around, but I think the following code should work.
fid = fopen('13V.csv');
C = textscan(fid,'%f%f','HeaderLines',8,'Delimiter',',','EndOfLine','\r\n','ReturnOnError',false);
fclose(fid);
[Time,Voltage] = C{:}
If this doesn't work for you, please contact customer support so we can investigate the issue. If it does, follow the link below and install that patch, I think it will make your life easier.
The original error message kind of makes me think this file has mixed line endings, and/or you're running into a known issue in DLMREAD.
Thanks,
Jeremy
Walter Roberson
le 27 Mai 2015
>> fid = fopen('13V.csv','rt')
fid =
3
>> C = textscan(fid,'%f,%f','HeaderLines',8)
C =
[500x1 double] [500x1 double]
You do not need to specify the Delimiter because of the explicit comma in the format. %f keeps scanning until it encounters something that cannot form a valid number, and then it "puts back" the character that caused the field to terminate. So the reading proceeds until the comma in the file. Then the comma in the format matches the comma in the file and the character is discarded. That leaves you ready to scan the next field with %f.
On my OS-X system, if I did not use 'rt' when I opened, textscan() failed unless I used EndOfLine, but when I did use 'rt' then there was no problem even without EndOfLine. It is possible that a different operating system would have a different reaction.... but it shouldn't happen.
I did miss out on the "fid" when I typed in the textscan() command though.
The sample file does not have mixed line endings.
s = fileread('13V.csv');
cellfun(@(V) V+0, regexp(s, '.$', 'match', 'dotexceptnewline', 'lineanchors'))
the result of all 13's shows that in every case the character before newline was char(13) which is carriage-return.
Marius Benjamin Aristide Paganel
le 3 Déc 2020
May also be a .csv format problem, verify how you save it.
Catégories
En savoir plus sur Large Files and Big Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!