19

I am trying to read in a .csv file with MATLAB. Here is my code:

csvread('out2.csv')

This is what out2.csv looks like:

03/09/2013 23:55:12,129.32,129.33
03/09/2013 23:55:52,129.32,129.33
03/09/2013 23:56:02,129.32,129.33

On windows I am able to read this exact same file with the xlsread function with no problems. I am currently on a linux machine. When I first used xlsread to read the file I was told "File is not in recognized format" so I switched to using csvread. However, using csvread, I get the following error message:

Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 2u) ==> /09/2013
23:55:12,129.32,129.33\n

Error in csvread (line 48)
m=dlmread(filename, ',', r, c)

I think the '/' in the date is causing the issue. On windows, the 1st column is interpreted as a string. On linux it seems to be interpreted as a number, so it tries to read the number and fails at the backslash. This is what I think is going on at least. Any help would be really appreciated.

1
  • 1
    try readtext instead of csvread. Commented Oct 27, 2013 at 0:07

4 Answers 4

31

csvread can only read doubles, so it's choking on the date field. Use textscan.

fid = fopen('out2.csv');
out = textscan(fid,'%s%f%f','delimiter',',');
fclose(fid);

date = datevec(out{1});
col1 = out{2};
col2 = out{3};

Update (8/31/2017)

Since this was written back in 2013, MATLAB's textscan function has been updated to directly read dates and times. Now the code would look like this:

fid = fopen('out2.csv');
out = textscan(fid, '%{MM/dd/uu HH:mm:ss}D%f%f', 'delimiter', ',');
fclose(fid)

[date, col1, col2] = deal(out{:});

An alternative as mentioned by @Victor Hugo below (and currently my personal go-to for this type of situation) would be to use readtable which will accept the same formatting string as textscan but assemble the results directly into a table object:

dataTable = readtable('out2.csv', 'Format', '%{MM/dd/uu HH:mm:ss}D%f%f')
dataTable.Properties.VariableNames = {'date', 'col1', 'col2'};

dataTable =

  3×3 table

           date             col1      col2 
    ___________________    ______    ______

    03/09/2013 23:55:12    129.32    129.33
    03/09/2013 23:55:52    129.32    129.33
    03/09/2013 23:56:02    129.32    129.33
Sign up to request clarification or add additional context in comments.

Comments

6

Unfortunately, the documentation for csvread clearly states:

M = csvread(filename) reads a comma-separated value formatted file, filename. The file can only contain numeric values.

Since / is neither a comma, nor a numeric value, it produces an error.

Comments

2

You can use readtable, as it will accept any input.

https://www.mathworks.com/help/matlab/ref/readtable.html

Comments

0

Yeah xlsread requires Microsoft Excel to be installed, unless it is run in 'basic' mode and 'basic' mode only reads .xls .xlsx and .xlsm files.

Another alternative are a number of user-written functions posted on MATLAB's file exchange that will work on linux and are more flexible at reading non formatted content.

One example: https://www.mathworks.com/matlabcentral/fileexchange/75219-csv2cellfast-import-csv-files-on-machines-without-excel

1 Comment

This was published as an answer, but does not attempt to answer the question. Please edit your answer and add an explanation that may be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.