user3781757
user3781757

Reputation: 15

Remove lines from a .txt with a certain character in MATLAB

I am working on a project with the data of magnitude of variable stars and I have come across a problem:

The data I have in a .txt document has a lot of comments marked with the symbol #, I would like to know if there is a way to read the whole text and take just the lines that does not include this particular character, I have already read the whole text and put into an array:

fid=fopen('000006+2553.txt','r');
i=1;
while 1
    tline=fgetl(fid);
    if ~ischar(tline), break, end
    A{i}=tline;
    i=i+1;
end

but from there I don't know how to follow.

Upvotes: 0

Views: 467

Answers (2)

bdecaf
bdecaf

Reputation: 4732

just add a

if tline(1)=='#', continue, end

to your loop. That's quite a standard snippet.

note I just check the first character on purpose, as I have come across data files that contain comments after data in the same line. Also (valid) string fields might include the character.

Upvotes: 1

Marcin
Marcin

Reputation: 238081

You can use strfind to check if a line contains #, for example

 if ~isempty(strfind(tline, '#'))
     continue;
 end  

Upvotes: 0

Related Questions