How to read csv files in matlab as you would in R?
Asked Answered
L

3

8

I have a data set that is saved as a .csv file that looks like the following:

Name,Age,Password
John,9,\i1iiu1h8
Kelly,20,\771jk8
Bob,33,\kljhjj

In R I could open this file by the following:

X = read.csv("file.csv",header=TRUE)

Is there a default command in Matlab that reads .csv files with both numeric and string variables? csvread seems to only like numeric variables.

One step further, in R I could use the attach function to create variables with associated with teh columns and columns headers of the data set, i.e.,

attach(X)

Is there something similar in Matlab?

Lamasery answered 23/1, 2013 at 20:24 Comment(3)
Exact duplicate of import csv to matlab with mixed data types.Aircraft
The last part is a subtle but important misrepresentation of what attach does in R. It only exposes the column names to the enclosing environment. The difference is crucial since alterations to those variables will not persist when detach(X) is executed. The use of attach is discouraged.Firstborn
@NathanG I would agree this is close to being an exact duplicate - but I don't think it is exact due to the extra question asking about attaching a header to a dataset. I've chosen to provide an answer anyway, as I personally would use textscan to solve this problem, not xlsread. The linked answer provides very little information on textscan - just a link to the documentation really.Storer
S
6

Although this question is close to being an exact duplicate, the solution suggested in the link provided by @NathanG (ie, using xlsread) is only one possible way to solve your problem. The author in the link also suggests using textscan, but doesn't provide any information about how to do it, so I thought I'd add an example here:

%# First we need to get the header-line
fid1 = fopen('file.csv', 'r');
Header = fgetl(fid1);
fclose(fid1);

%# Convert Header to cell array
Header = regexp(Header, '([^,]*)', 'tokens');
Header = cat(2, Header{:});

%# Read in the data
fid1 = fopen('file.csv', 'r');
D = textscan(fid1, '%s%d%s', 'Delimiter', ',', 'HeaderLines', 1);
fclose(fid1);

Header should now be a row vector of cells, where each cell stores a header. D is a row vector of cells, where each cell stores a column of data.

There is no way I'm aware of to "attach" D to Header. If you wanted, you could put them both in the same structure though, ie:

S.Header = Header;
S.Data = D;
Storer answered 23/1, 2013 at 22:8 Comment(1)
You can "attach" them by using cell arrays and making the header the first row of your cell array, just like it was done in the .csv file. The only time I do this is when I am going to write the resulting cell array to Excel. It might seem trivial in other contexts, but I think it is worth noting.Sudan
N
3

Matlab's new table class makes this easy:

X = readtable('file.csv');

By default this will parse the headers, and use them as column names (also called variable names):

>> x

x = 

 Name      Age     Password  
_______    ___    ___________

'John'      9     '\i1iiu1h8'
'Kelly'    20     '\771jk8'  
'Bob'      33     '\kljhjj' 

You can select a column using its name etc.:

>> x.Name

ans = 

    'John'
    'Kelly'
    'Bob'

Available since Matlab 2013b. See www.mathworks.com/help/matlab/ref/readtable.html

Nickelodeon answered 12/2, 2016 at 5:23 Comment(0)
E
0

I liked this approach, supported by Matlab 2012.

path='C:\folder1\folder2\';
data = 'data.csv';
data = dataset('xlsfile',sprintf('%s\%s', path,data));

Of cource you could also do the following:

[data,path] = uigetfile('C:\folder1\folder2\*.csv');
data = dataset('xlsfile',sprintf('%s\%s', path,data));
Embryo answered 29/3, 2013 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.