Trying to extract data from a csv file
18 views (last 30 days)
Show older comments
Hello, I'd like to create a data logging system that extracts a certain cell range from an excel file, but I'm running into trouble.
Here is my very simple code:
filename = 'mydata.csv';
M = csvread('mydata.csv',3,4,[3,4,22,4])
Here is the error I am getting:
Error using dlmread (line 147)
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 1,
field number 7) ==> hours instead of 24,\n
Error in csvread (line 50)
m=dlmread(filename, ',', r, c, rng);
Error in Untitled (line 2)
M = csvread('lap shear data.csv',3,4,[3,4,22,4])
Does anyone know why this is happening? I just want to extract the column that has the numbers (Column 5).. Not sure why it's trying to read the next one over.
Thanks in advance!!
2 Comments
Stephen23
on 21 Jan 2017
@Anahita Mahmoodi: please edit your question and upload a sample file by clicking the paperclip button.
Then we can actually run and test code using your file. Testing code using a screenshot is impossible.
Accepted Answer
Walter Roberson
on 21 Jan 2017
Older versions of csvread cannot be used with files with any text, not even in header lines. Somewhere around R2014a it started being able skip text in header lines, and if I recall, also that it could also skip text in row headers (in both cases, it had to be told to skip them.)
However, those newer versions still cannot handle trailing columns of text. You will not be able to read that file format with csvread.
I suggest that you try readtable first. If that does not work then you will need to code a format and use textscan
5 Comments
Walter Roberson
on 22 Jan 2017
Notice that I did not put a space in the variable name. readtable removes the space when it converts the column headers into table column names.
I took your xls and had excel save as csv and tested with that. The only difference between the code that I used on my system and the code I posted is that I had added a 1 to the file name when I saved it and I removed that 1 from my post
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!