Importing csv file in matlab

5 次查看(过去 30 天)
Nicola Benenati
Nicola Benenati 2015-2-17
回答: dpb 2015-2-17
hello
i'm trying to import a csv file in matlab, i used the dlmread function but it gives to me this error
M = dlmread('prueba2.csv', ',', [1 5 1 8])
Error using dlmread (line 138)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 7u) ==> :00:00.009996, -0,971, 0,236, 0,007,,00:00:00.009996,
-994, 242, 7\n
the csv file contains values of an accelerometer i write down some row of it
Time (s), X-Axis (raw), Y-Axis (raw), Z-Axis (raw),,Time (s), X-Axis (g), Y-Axis (g), Z-Axis (g),,Time (s), X-Axis (c), Y-Axis (c), Z-Axis (c)
00:00:00.009996, 3102, 242, 7,,00:00:00.009996, -0,971, 0,236, 0,007,,00:00:00.009996, -994, 242, 7
00:00:00.019993, 3102, 242, 7,,00:00:00.019993, -0,971, 0,236, 0,007,,00:00:00.019993, -994, 242, 7
00:00:00.030003, 3100, 241, 6,,00:00:00.030003, -0,973, 0,235, 0,006,,00:00:00.030003, -996, 241, 6
i have to extract just the value from the 5th to the 8th column
why matlab gives to me an error ?

回答(2 个)

Andy
Andy 2015-2-17
dlmread is for numeric only files. In your file you have text in the first row. Have you tried readtable ?

dpb
dpb 2015-2-17
Per the doc's
>> help dlmread
dlmread Read ASCII delimited file.
...
RESULT = dlmread(FILENAME,DELIMITER) reads numeric data from the ASCII
delimited file FILENAME using the delimiter DELIMITER. The result is
returned in RESULT. Use '\t' to specify a tab.
...
All data in the input file must be numeric. dlmread does not operate
on files containing nonnumeric data, even if the specified rows and
columns for the read contain numeric data only.
Use textscan instead. Also, it appears you mean to read the sixth thru ninth columns; the fifth is an empty column.
fmt=[repmat('%*s',1,5) '%s' repmat('%f',1,3) '%*[^\n]'];
fid=fopen('prueba2.csv');
c=textscan(fid,fmt,'headerlines',1,'delimiter',',','collectoutput',1);
fid=fclose(fid);
You'll get a cell array containing the time stamp as character string and the three-axis accel's as an array. Use datenum to convert the string to date numbers for plotting and the like.

类别

Help CenterFile Exchange 中查找有关 Large 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