csvread returns matrix with zeros

I am using Ocatve and I have a problem using csvread. The CSV file I want to read isn't empty. But when I try reading it into a matrix I got a matrix all filled with zeros
Here is my Octave file :
user_file = "BioData_game0_user39.csv"
J = csvread(user_file)
and this is the result I got when running the .m file:
source ("csvreader.m")
>>>user39_file = BioData_game0_user39.csv
J =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

2 Comments

Can you post a sample of data in your csv file ?
Akari
Akari on 5 Aug 2013
Edited: Akari on 5 Aug 2013
"2","0","228.5","442","0.6875","0.333333","0","0"
"2","0","227.75","442","0.6875","0.333333","16","1"
"2","0","227.75","442","0.6875","0.333333","48","2"
"2","0","550.5","539.5","0.6375","0.266667","1168","0"
"2","0","549","536.5","0.6375","0.266667","1184","1"
"2","0","549","536.5","0.6375","0.266667","1200","2"

Sign in to comment.

 Accepted Answer

Jan
Jan on 5 Aug 2013
Edited: Jan on 5 Aug 2013
I see a lot of double quotes in your file. It is free guessing only, but I'd assume, that cvsread cannot handle them correctly. textread can ignore the double-quotes and you can import the data by sscanf also, if you know the number of columns:
FID = fopen(FileName, 'r');
if FID == -1, error('File not found'); end % [EDITED]
Fmt = repmat('"%g",', 1, 8);
Fmt(end) = [];
Data = fscanf(FID, Fmt, [8, Inf]); % [EDITED] sscanf -> fscanf
fclose(FID);

8 Comments

Akari
Akari on 5 Aug 2013
Edited: Akari on 5 Aug 2013
Ok, but the first argument of sscanf should be a string, right ? Besides, Why shoud I use it instead of the usual scanf ?
Is textread working with CSV files ?
@Akari: Typo fixed. textread is working with any kind of text files.
@Jan Simon: I have a error with the fscanf :
>>>error: fscanf: invalid stream number = -1
error: called from:
error: csvreader.m at line 9, column 6
This means that the specified file is not existing.
This solution should not be working actually (unless I misunderstand something essential), because FSCANF is likely to stop reading at the end of the first row.
I think that a small pretreatment is required before using F/SCANF, e.g. in the following small variation of Jan's solution:
buffer = fileread('myFile.csv') ;
buffer = regexprep(buffer, '\r\n|\n', ',') ;
data = sscanf(buffer, '"%f",', [8, Inf]).' ;
I expect fscanf to ignore the line breaks. The example from the documentation does not stop at the line break also: http://www.mathworks.com/help/matlab/ref/fscanf.html.
Interesting; I just tested on my 2012b to be sure and it does stop. There was then some improvement or change of behavior between this version and 2013a..
EDIT: still on 2012b, all of the following
Fmt = '"%g","%g","%g","%g","%g","%g","%g","%g"\r\n' ;
Data = fscanf(FID, Fmt, [8, Inf]).';
Fmt = '"%g","%g","%g","%g","%g","%g","%g","%g"\n' ;
Data = fscanf(FID, Fmt, [8, Inf]).';
Fmt = '"%g","%g","%g","%g","%g","%g","%g","%g"\r' ;
Data = fscanf(FID, Fmt, [8, Inf]).';
do work, but not the following
Fmt = '"%g","%g","%g","%g","%g","%g","%g","%g"' ;
Data = fscanf(FID, Fmt, [8, Inf]).';

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 4 Aug 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!