Removing certain lines in a text file by setting a restriction
이전 댓글 표시
I am trying to remove certain lines of text in a file by setting the restriction that the 15th column of the each line can only go up to the numerical value of '21'.
For example:
13|PGC000013|0.00370|33.13420|~|15.41|0.675|0.217|0.587|~|0.87|0.102|~|-18.94|72.722|10.908|0.40|0.41|
has a value of '72.722', which is more than the '21' cutoff threshold so it would be eliminated.
My file is attached.
채택된 답변
추가 답변 (1개)
Azzi Abdelmalek
2015년 6월 17일
fid=fopen('fic.txt')
l=fgetl(fid);
k=1;
while ischar(l)
r{k}=l;
k=k+1
l=fgetl(fid);
end
kk=0
for k=1:numel(r)
a=str2double(regexp(r{k},'-?\d+(\.\d+)?','match'));
if a(5)<21
kk=kk+1;
out{kk}=r{k};
k=k+1;
end
end
댓글 수: 9
jgillis16
2015년 6월 17일
Azzi Abdelmalek
2015년 6월 17일
I tested your attached file, and there is no errors. try to clear your variables
clear
jgillis16
2015년 6월 17일
jgillis16
2015년 6월 17일
Azzi Abdelmalek
2015년 6월 17일
fid=fopen('fic.txt');
fid1=fopen('fic1.txt','w');
l=fgetl(fid);
k=1;
while ischar(l)
r{k}=l;
a=str2double(regexp(r{k},'-?\d+(\.\d+)?','match'));
if a(5)<21
kk=kk+1;
out{kk}=r{k};
fprintf(fid1,'%s\r\n',out{kk});
end
l=fgetl(fid);
k=k+1;
end
kk=0
fclose(fid)
fclose(fid1)
jgillis16
2015년 6월 17일
Azzi Abdelmalek
2015년 6월 17일
This is not correct, in your previous comment you said it works and asked how to save in a new text file, that's what this code do
jgillis16
2015년 6월 18일
Azzi Abdelmalek
2015년 6월 18일
No, they are saved in the file 'fic1.txt'
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!