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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    Exact duplicate of [import csv to matlab with mixed data types](http://stackoverflow.com/questions/9248789/matlab-how-to-import-multiple-csv-files-with-mixed-data-types?rq=1). – N8TRO Jan 23 '13 at 20:30
  • 2
    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. – IRTFM Jan 23 '13 at 20:53
  • @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. – Colin T Bowers Jan 23 '13 at 22:12

3 Answers3

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;
Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
  • 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. – dgoverde Feb 12 '16 at 06:57
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

mjeppesen
  • 1,040
  • 1
  • 10
  • 14
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));
Stanislav
  • 2,629
  • 1
  • 29
  • 38