Matlab failing to import data

119 ビュー (過去 30 日間)
Jorge Costa
Jorge Costa 2015 年 5 月 26 日
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 件のコメント
Jorge Costa
Jorge Costa 2015 年 5 月 26 日
Hi Ingrid, the file is now attached - apologies.
Your suggestion sounds plausible. How would I go about checking this?
Jorge Costa
Jorge Costa 2015 年 5 月 27 日
Just checked and all the regional settings (as far as I can tell) are the same on both machines.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2015 年 5 月 26 日
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 件のコメント
Wan Noor
Wan Noor 2017 年 9 月 27 日
how to write it using csvread without using xlsread
Walter Roberson
Walter Roberson 2017 年 9 月 27 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 5 月 26 日
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 件のコメント
Walter Roberson
Walter Roberson 2015 年 5 月 27 日
>> 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
Marius Benjamin Aristide Paganel 2020 年 12 月 3 日
May also be a .csv format problem, verify how you save it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by