How to select rows with a particular value form one text file and write desired rows in new file?

2 次查看(过去 30 天)
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.

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-11-29
编辑:KALYAN ACHARJYA 2018-11-29
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

更多回答(1 个)

Bob Thompson
Bob Thompson 2018-11-29
编辑:Bob Thompson 2018-11-29
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.

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by