Reading Csv Files with Matlab?

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

4 Answers

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

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.

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

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:

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest