views:

334

answers:

3

Is there any character or character combination that MATLAB interprets as comments, when importing data from text files? Being that when it detects it at the beginning of a line, will know all the line is to ignore?

I have a set of points in a file that look like this: alt text And as you can see he doesn't seem to understand them very well. Is there anything other than // I could use that MATLAB knows it's to ignore?

Thanks!

+1  A: 

Have you tried %, the default comment character in MATLAB?

As Amro pointed out, if you use importdata this will work.

Tomas Lycken
I'll try it now.
devoured elysium
Doesn't seem to.
devoured elysium
+3  A: 

If you use the function textscan, you can set the CommentStyle parameter to // or %. Try something like this:

fid = fopen('myfile.txt');
iRow = 1;
while (~feof(fid)) 
    myData(iRow,:) = textscan(fid,'%f %f\n','CommentStyle','//');
    iRow = iRow + 1;
end
fclose(fid);

That will work if there are two numbers per line. I notice in your examples the number of numbers per line varies. There are some lines with only one number. Is this representative of your data? You'll have to handle this differently if there isn't a uniform number of columns in each row.

Scottie T
Doesn't seem to work. I edited the original post.
devoured elysium
I put them with 2 numbers per line(as they will always be) and still doesn't work.
devoured elysium
It's reading one line at a time. I modified my response to read to the end of the file.
Scottie T
Thanks! That's perfect!
devoured elysium
you can do this without loops..
Amro
+5  A: 

Actually, your data is not consistent, as you must have the same number of column for each line.

1)

Apart from that, using '%' as comments will be correctly recognized by importdata:

file.dat

%12 31
12 32
32 22
%abc
13 33
31 33
%ldddd
77 7
66 6
%33 33
12 31
31 23

matlab

data = importdata('file.dat')

2)

Otherwise use textscan to specify arbitrary comment symbols:

file2.dat

//12 31
12 32
32 22
//abc
13 33
31 33
//ldddd
77 7
66 6
//33 33
12 31
31 23

matlab

fid = fopen('file2.dat');
data = textscan(fid, '%f %f', 'CommentStyle','//', 'CollectOutput',true);
data = cell2mat(data);
fclose(fid);
Amro