필터 지우기
필터 지우기

Using textread and strfind to extract lines of data

조회 수: 2 (최근 30일)
Paul Huter
Paul Huter 2012년 11월 27일
I have used textread to load a data file into memory. From here, I want to extract specific lines that contain a particular string. I imagine I can do this with strfind, but the documentation indicates it will only return the start index of the string. How can I extract the whole line of data?
  댓글 수: 1
Jonathan Epperl
Jonathan Epperl 2012년 11월 27일
You probably used textread to load data into a variable, and if you didn't, then you should do so. Also you should give some more detail: What does your data look like, what do you mean by 'line', is your data separated by line breaks?

댓글을 달려면 로그인하십시오.

채택된 답변

Paul Huter
Paul Huter 2012년 11월 27일
This is what I came up with:
fo = fopen('out.dat', 'wt')
t = textread('input.dat', '%s', 'delimiter', '\n', 'bufsize', 1048576);
c = 1;
for z = 1:1:length(t)
s = strfind(t{z}, string)
if ~isempty(s)
fprintf(fo, t{z});
fprintf(fo, '\n');
c = c + 1;
clear s
end
end
fclose(fo);
Seems to work quite well.
  댓글 수: 1
Jan
Jan 2012년 11월 27일
There is no need to "clear s". "fprintf(fo, t{z})" will lead to errors, if the string contains a % character. Better:
fprintf(fo, '%s\n', t{z});

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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