Removing certain lines in a text file by setting a restriction
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 17 de Jun. de 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 comentarios
jgillis16
el 17 de Jun. de 2015
Azzi Abdelmalek
el 17 de Jun. de 2015
I tested your attached file, and there is no errors. try to clear your variables
clear
jgillis16
el 17 de Jun. de 2015
jgillis16
el 17 de Jun. de 2015
Azzi Abdelmalek
el 17 de Jun. de 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
el 17 de Jun. de 2015
Azzi Abdelmalek
el 17 de Jun. de 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
el 18 de Jun. de 2015
Azzi Abdelmalek
el 18 de Jun. de 2015
No, they are saved in the file 'fic1.txt'
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!