gstar2002
gstar2002

Reputation: 495

why textscan only read one line

I am trying to read a csv file use textscan. The fields are seperated with ',' . I used the following code, but it only read in one line of data into the matrix W.

I also tried dlmread(), it got the number of fields wrong.

The file is contructed under linux, matlab is under linux.

file_id = fopen('H:\data\overlapmatrices\cos.mat.10');
W = textscan(file_id, '%f', 'delimiter', ',' , 'EndOfLine', '\r\n');
fclose(file_id);
clear file_id;

Upvotes: 0

Views: 4134

Answers (2)

John Manak
John Manak

Reputation: 13558

The problem could be in how the end of line is represented in the file (see also this article on Wikipedia). While \r\n (the combination of a carriage return and a newline character) is common on Windows, \n (just the newline character) is the standard on Linux and other Unix systems.

But as ben is saying, csvread might be an easier way how to read the file.

Upvotes: 1

ben
ben

Reputation: 1390

you might wanna try csvread, it should do the trick.

or you could alway do something dirty like

fid = fopen( filename );
tline = fgetl(fid);
while ischar(tline) %or some other check
    %sscanf(tline...
    tline = fgetl(fid);    
end

Upvotes: 1

Related Questions