Removing certain lines in a text file by setting a restriction
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (1)
Azzi Abdelmalek
am 17 Jun. 2015
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 Kommentare
jgillis16
am 17 Jun. 2015
Azzi Abdelmalek
am 17 Jun. 2015
I tested your attached file, and there is no errors. try to clear your variables
clear
jgillis16
am 17 Jun. 2015
jgillis16
am 17 Jun. 2015
Azzi Abdelmalek
am 17 Jun. 2015
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
am 17 Jun. 2015
Azzi Abdelmalek
am 17 Jun. 2015
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
am 18 Jun. 2015
Azzi Abdelmalek
am 18 Jun. 2015
No, they are saved in the file 'fic1.txt'
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!