필터 지우기
필터 지우기

how to escape,reading line that starts with special character in a text file.

조회 수: 5 (최근 30일)
Aditya
Aditya 2014년 1월 21일
답변: Walter Roberson 2014년 1월 21일
$hi jhon
$hey
$hello
hello

답변 (2개)

kjetil87
kjetil87 2014년 1월 21일
편집: kjetil87 2014년 1월 21일
fid=fopen(filename);
if(fid<0);error('unable to open file');end
y=cell(500,1);%allocate some space, if you allocate less then the end
%result it will take more time to read the file.
cntr = 1;
while (true)
line = fgetl(fid);
if(line==-1);break; end; %-1 indicates end of file...
if(line(1)=='$')
continue;
end
y{cntr} = line;
cntr = cntr +1;
end
y=y(1:cntr-1);
fclose(fid);
Something like this maybe? I guess you could also read the entire file and then toss lines that starts with '$' , which in effect is what i am doing here aswell. But meh...

Walter Roberson
Walter Roberson 2014년 1월 21일
textscan() has CommentStyle that allows you to set a string that indicates that everything after the string is a comment. But that would affect more than just the first column.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by