Probelm importing csv file with csvread

1 view (last 30 days)
Konstantinos
Konstantinos on 19 Jun 2014
Answered: Konstantinos on 19 Jun 2014
Hi,
I am trying to import a csv file to my matlab code but something seems to go wrong with the way I am trying to do it. The command that I type is
M = csvread('reoprt_data1.csv',7,1);
Because I want to import a specific range of measurements to a matrix and I get the following answer:
Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 1u, field 77u) ==> nity,,0.0,0.0,3.337845264468342E-4,,50.0,0.0,3.337845264468342E-4,,0.0,-3.337845264468342E-4,,0.0,-0.0,148.73287963867188,,0.0,-148.73287963867188,,0.0,Infinity,,\n
Error in csvread (line 48) m=dlmread(filename, ',', r, c);
Error in test (line 6) M = csvread('reoprt_data2.csv',7,1);
Can you please give me some help on that?
Thank you in advance.

Answers (3)

Image Analyst
Image Analyst on 19 Jun 2014
You forgot to attach your csv file. It looks like there are double commas and strings in there. I don't think it can handle that. Try fixing your file. You can call readtable() if you have strings and numbers on the same line.

dpb
dpb on 19 Jun 2014
(row 1u, field 77u) ==> nity,,0.0,0.0,3.33...42E-4,,50.0,0.0,...0.0,Infinity,,\n
The recognized value is 'Inf', not 'Infinity'
Your choice is to either create the file with the corrected indicator value or you can try and see if textscan can manage with the 'treatasempty' field altho I've not tested.
Other than that, will have to read as text and parse each line; the way I'd do it would be to read the entire file as a cell array of strings and do a global substitution of 'Inf' for 'infinity' then use read from the memory array w/ sscanf()

Konstantinos
Konstantinos on 19 Jun 2014
Thank you very much for your quick reply, I currently don't have much time to elaborate on the textscan command so I used the readtable() I manually deleted the column that didn't have numeric values and saved back to a csv file and it works!
Thank you again

Tags

Community Treasure Hunt

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

Start Hunting!