G Gr
G Gr

Reputation: 6080

Matlab read csv string array

I have a comma seperated dataset called Book2.csv I want to extract the contents. The contents are a 496024x1 array of strings (normal, neptune, smurf).

I tryed:

 [text_data] = xlsread('Book2.csv');

But it just outputed a text_data empty array?

When trying csvread

M = csvread('Book2.csv')
??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==>
norma

Error in ==> csvread at 54
    m=dlmread(filename, ',', r, c);

I get this error. Can anyone help?

Upvotes: 0

Views: 14897

Answers (3)

Ben A.
Ben A.

Reputation: 1039

Off the top of my head this should get the job done. But possibly not the best way to do it.

fid = fopen(your file);  //open file
 //read all contents into data as a char array 
 //(don't forget the `'` to make it a row rather than a column).
data = fread(fid, '*char')';
fclose(fid);
//This will return a cell array with the individual
//entries for each string you have between the commas.
entries = regexp(data, ',', 'split'); 

Upvotes: 4

Stanislav
Stanislav

Reputation: 2677

The easiest way for me is :

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

now you will have loaded the data as dataset. An easy way to get a column 1 for example is

 double(data(1))

Upvotes: 0

NominSim
NominSim

Reputation: 8511

Try something like: textread

data = textread('data.csv', '', 'delimiter', ',', ... 
            'emptyvalue', NaN);

Upvotes: 0

Related Questions