How to select rows with a particular value form one text file and write desired rows in new file?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marisabel Gonzalez
el 29 de Nov. de 2018
Editada: KALYAN ACHARJYA
el 29 de Nov. de 2018
Let's say I have the following data
3 43.45 0.5
4 23.21 0.4
5 32.84 0.3
6 67.02 0.4
2 13.00 0.2
3 23.32 0.1
How would I only select the rows in which the 3rd column elements are less than 0.4 and write them into a new file?
Then in this new file I would only have row 3,5 and 6 in this new file.
0 comentarios
Respuesta aceptada
KALYAN ACHARJYA
el 29 de Nov. de 2018
Editada: KALYAN ACHARJYA
el 29 de Nov. de 2018
k=load('test1.txt');
idx=k(:,3)<0.4;
k(idx,:)
%Next save the file as new name, hope you can do that.
%Still if you need help, comment below
ans =
5.0000 32.8400 0.3000
2.0000 13.0000 0.2000
3.0000 23.3200 0.1000
0 comentarios
Más respuestas (1)
Bob Thompson
el 29 de Nov. de 2018
Editada: Bob Thompson
el 29 de Nov. de 2018
I'm assuming the file only has numeric data.
data = dlmread('yourfilename.txt');
datan = data(data(:,3)<0.4,:);
dlmwrite('yournewfilename.txt',datan,'delimiter',' ');
You can change precision, by adding 'precision' and some form of precision (similar to fprintf), to the dlmwrite command. This will allow you to have extra zeroes, or spaces or things of that nature.
0 comentarios
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!