Importing csv file in matlab

7 views (last 30 days)
Nicola Benenati
Nicola Benenati on 17 Feb 2015
Answered: dpb on 17 Feb 2015
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 ?

Answers (2)

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

dpb
dpb on 17 Feb 2015
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.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!