extracting all lines includes specific string from text file
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
sermet OGUTCU
am 6 Jul. 2021
Kommentiert: sermet OGUTCU
am 6 Jul. 2021
I have a data file (the attached one). I used the following codes to read this file.
[FileName,pathname,d] = uigetfile('*.txt');
full_file_name = fullfile(pathname,FileName);
Str = fileread(full_file_name);
C = strsplit(Str, '\n');
I need to extract all lines includes "999999.99999" strings in this text file. The related line number starts with 34014 and ends with 34130 for this file. How can I extract these lines and store them as the following
PG01 22155.578198 14080.242263 4873.647728 999999.999999
PG02 -8412.758135 -19698.690630 16334.823775 999999.999999
.
.
.
PJ03 -33331.824920 16686.676303 -15088.023498 999999.999999
0 Kommentare
Akzeptierte Antwort
Rik
am 6 Jul. 2021
%If you don't use the readfile function, you can use readlines instead
readfile=@(fn)cellstr(readlines(fn));%(requires >=R2020a)
data=readfile('https://www.mathworks.com/matlabcentral/answers/uploaded_files/675913/COD0MGXFIN_20210870000_01D_05M_ORB.txt');
newdata=data(cellfun(@(x) contains(x,'999999.99999'),data))
Now you can easily write it with fprintf:
fid=fopen('myfile.txt','w');
fprintf(fid,'%s\n',newdata{:}); % this will introduce a trailing newline
fclose(fid);
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Import and Export finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!