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
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
Jorge Costa le 26 Mai 2015
Hi Ingrid, the file is now attached - apologies.
Your suggestion sounds plausible. How would I go about checking this?
Jorge Costa
Jorge Costa le 27 Mai 2015
Just checked and all the regional settings (as far as I can tell) are the same on both machines.

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 26 Mai 2015

1 vote

We don’t have your file, but the problem may be with the ‘8 lines of blurb’. The dlmread and csvread functions (at least to the best of my knowledge) like files with only numeric data. I would use textscan instead, with 'HeaderLines',8 and appropriate format discriptor, 'Delimiter', and if necessary, 'EndOfLine' declarations.

8 commentaires

Jorge Costa
Jorge Costa le 26 Mai 2015
Modifié(e) : Jorge Costa le 26 Mai 2015
Thanks for your answer, Star Strider! I have also tried textscan with Headerlines, 8 etc. but that did not work either. I really don't think this is a script issue as importing the data manually also doesn't work as the attached file in the original post shows.
edit: File now attached, apologies.
Star Strider
Star Strider le 26 Mai 2015
I was really hoping for the file itself, not a PDF of the screenshot.
Jorge Costa
Jorge Costa le 26 Mai 2015
I see - the file is now attached to this answer.
When in doubt, click on it and see what (if any) application opens it. This one’s an Excel .csv file, so xlsread will read it without complaint:
[n,s,r] = xlsread('Jorge Costa 13V.csv');
figure(1)
plot(n(:,1), n(:,2))
xlabel('Time (s)')
ylabel('Voltage (V)')
grid
Jorge Costa
Jorge Costa le 27 Mai 2015
Fantastic. This works! :) Out of curiosity, why would the 'Import Data' button fail to work in this instance?
Star Strider
Star Strider le 27 Mai 2015
Thank you!
I'm not certain. I didn’t try it with the Import Data Wizard since I was able to import it with xlsread. It seems to be a straightforward file, but it may contain some idiosyncratic features that the Import Data Wizard has problems with.
Wan Noor
Wan Noor le 27 Sep 2017
how to write it using csvread without using xlsread
Walter Roberson
Walter Roberson le 27 Sep 2017
From somewhere around R2015a (we are not sure of the exact release), you can tell csvread to skip text headers by passing it R and C values. The R should be the number of lines to skip, not the line number to start reading at. The C value should be 0.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 26 Mai 2015

0 votes

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
Jorge Costa le 27 Mai 2015
Thanks for your answer, Walter. I had previously tried textscan with no success and it still doesn't work I'm afraid. Here C returns a 1x0 cell and when I try to call C{1} or C{2} i receive the error 'Index exceeds matrix dimensions'.
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
>> 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.
May also be a .csv format problem, verify how you save it.

Connectez-vous pour commenter.

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!

Translated by