Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading CSV files with MATLAB?

Tags:

csv

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.

like image 826
casandra Avatar asked Oct 26 '13 23:10

casandra


People also ask

How do I read a CSV file in MATLAB?

M = csvread( filename ) reads a comma-separated value (CSV) formatted file into array M . The file must contain only numeric values. M = csvread( filename , R1 , C1 ) reads data from the file starting at row offset R1 and column offset C1 . For example, the offsets R1=0 , C1=0 specify the first value in the file.

Can MATLAB import CSV?

Launch MATLAB and click “File” in the menu bar at the top of the window. Click “Set Path” and search the pop-up file browser for the folder to set as your MATLAB path variable. Alternatively, leave the path set to the default folder. In Windows Explorer, drag and drop a CSV file in any folder on the MATLAB path.

How do I read multiple CSV files in MATLAB?

Accepted Answer You can use dir to read the name of all files with csv extensions. files = dir('*. csv');


3 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
like image 73
craigim Avatar answered Oct 22 '22 11:10

craigim


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.

like image 27
nispio Avatar answered Oct 22 '22 12:10

nispio


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

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

like image 2
Victor Hugo Avatar answered Oct 22 '22 11:10

Victor Hugo