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

1 view (last 30 days)
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.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 29 Nov 2018
Edited: KALYAN ACHARJYA on 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

More Answers (1)

Bob Thompson
Bob Thompson on 29 Nov 2018
Edited: Bob Thompson on 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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!