How to read specific lines from text file?

59 Ansichten (letzte 30 Tage)
Cat Koval
Cat Koval am 12 Jun. 2021
Kommentiert: dpb am 13 Jun. 2021
Hello, I have a little question. I have a text file that consists of four columns, and several lines, columns also have headers. And i woder how to read a specific line, i just want to read first line(with headers) and the third one, and export it to another file.
I'm lost and I don't know how to do this.
I'm sure that at the beginning I have to start with:
fid=fopen('filename.txt');
  4 Kommentare
Jan
Jan am 12 Jun. 2021
How do you determine, which lines you want to read? If you search for a specific value, you have to read all lines, of course. Otherwise you cannot check the contents.
Cat Koval
Cat Koval am 13 Jun. 2021
Bearbeitet: Cat Koval am 13 Jun. 2021
So, I have a file 'cor.txt':
Nr X Y Z
1 323.12 121.22 12.11
2 111.10 133.29 0
3 233.12 645.00 13.12
4 325.12 645.00 23.33
9 553.13 128.22 13.77
And I want to create new file with the first line (with headers) and the forth and fifth line where the Y=645.00
And I made up something like this
clc, clear
% open the file
fid2=fopen('cor.txt', 'r');
%reading all lines
line =fgetl(fid2)
line1 =fgetl(fid2);
line2 =fgetl(fid2);
line3 =fgetl(fid2)
line4 =fgetl(fid2)
%creating a new file with specific lines
fid=fopen('ikd.txt','w');
fprintf(fid, '%.19s\n ',line);
fprintf(fid, '%.19s\n ',line4);
fprintf(fid, '%.19s\n ',line5);
fclose (fid)
Okay.... its not advanced but it works, but now i wonder what if I'm in a situation where there is a lot more lines in file, can use a loop or sth, to find all lines with Y=645.00? And if i can, how to do it :(

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Scott MacKenzie
Scott MacKenzie am 13 Jun. 2021
No need for a loop. Since your data are rectangular and the data in each column are of the same type, you're best option is to use readtable combined with a logical expression to identify the rows of interest:
% read your data into a MATLAB table (includes header line)
T = readtable('corr.txt');
% identify rows of interest using logical expression
rowLogical = T{:,3} == 645;
% build new table containing header and only the rows of interest
Tnew = T(rowLogical,:);
% write new data in file
writetable(Tnew, 'idk.txt');
You can get fancy with logical expressions. For example, if you want to save all rows were the value in the Y column is > 600, then change the logical expression to
rowLogical = T{:,3} > 600;
For more examples on using logical expressions, see Find Array Elements That Meet a Condition
  1 Kommentar
dpb
dpb am 13 Jun. 2021
And, even more convenient with the table, you get the variable names for free along with the data so you can write logical expressions like
T = readtable('corr.txt');
WantedY = 645; % use variables, don't bury "magic" numbers inline in code
outfile='idk.txt'; % ditto, now can get new file by adding an input statement, not change code
Tn=T(T.Y==WantedY,:); % Scott's logic except use variable name(*) instead of position
writetable(T,outfile)
(*) And, you can write variable names as character variables, you don't have to hardcode them, necessarily, either.
Read the documenation on the table data class and the excellent tutorial material provided on how to reference data in tables..."let me count the ways!"

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by