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

3 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

KALYAN ACHARJYA
KALYAN ACHARJYA am 29 Nov. 2018
Bearbeitet: KALYAN ACHARJYA am 29 Nov. 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

Weitere Antworten (1)

Bob Thompson
Bob Thompson am 29 Nov. 2018
Bearbeitet: Bob Thompson am 29 Nov. 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.

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by