Reading CSV files with MATLAB?

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


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

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.


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',',');

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', ',');

[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.

Need Your Help

Change UIDatePicker font color?

ios objective-c fonts colors uidatepicker

All I want to do is change the font color of the UIDatePicker. I've researched other questions but they're all involving changing other properties and customizing the entire look. All I want to do is

How to programmatically create a new VPN interface with Android 4.0?

android vpn android-4.0-ice-cream-sandwich

I'd like to know whether it's possible to create a VPN interface programmatically with the new VPN APIs in Android 4.0. I've looked through but...