0

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?

3
  • 1
    did you try csvread? Commented Jul 16, 2012 at 15:39
  • Give my csvimport function a try :) Commented Jul 16, 2012 at 15:43
  • You should've provided file or at least part of it if you would like for someone to resolve the errors... Commented Feb 5, 2013 at 14:03

3 Answers 3

4

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'); 
Sign up to request clarification or add additional context in comments.

7 Comments

this outputs all the information in one column and one row, it doesnt list the strings from column 1, row 1 to row 496024?
In the sample code I put up, entries will be a single row. It will be indexable 1 to how ever many strings you had in the file.
how can I output entries to however many rows are in the csv file? This is a list of attributes so I need it as a list the same as the cvs file. Not just all clumped in one row.
Well, that's the downside here. fread will just read in everything as a string of characters. So no matter how many rows you have in the csv file you'll have one really long array of char. This will then just return you a long row of cells (say you have 3 rows of 100 elements) there will be 300 cells then.
A = textscan(fid,'%s','Delimiter', ','); This will just return a column of all the data that was in the file as a cell array. If you know the number of elements (and it's a rectangular matrix) you can use reshape. It would be like reshape(A{1}, m, n); where m and n are rows and columns, respectively.
|
0

Try something like: textread

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

1 Comment

??? Error using ==> dataread Trouble reading number from file (row 1, field 1) ==> normal.\n Error in ==> textread at 176 [varargout{1:nlhs}]=dataread('file',varargin{:});
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.