Trying to extract data from a csv file

18 visualizaciones (últimos 30 días)
GG
GG el 21 de En. de 2017
Comentada: Walter Roberson el 22 de En. de 2017
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 comentarios
Stephen23
Stephen23 el 21 de En. de 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.
GG
GG el 21 de En. de 2017
Hi stephen, I uploaded both matlab and excel files, i was forced to convert it to an xls but my code reflects a csv version of this. thanks

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de En. de 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 comentarios
GG
GG el 22 de En. de 2017
Thank you for the help!
Walter Roberson
Walter Roberson el 22 de En. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by