Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing text files with comments in MATLAB

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!

like image 787
devoured elysium Avatar asked Dec 08 '22 05:12

devoured elysium


1 Answers

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);
like image 137
Amro Avatar answered Dec 09 '22 19:12

Amro