extracting all lines includes specific string from text file

21 views (last 30 days)
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

Accepted Answer

Rik
Rik on 6 Jul 2021
You can get my readfile function from the FEX.
%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))
newdata = 117×1 cell array
{'PG01 22155.578198 14080.242263 4873.647728 999999.999999'} {'PG02 -8412.758135 -19698.690630 16334.823775 999999.999999'} {'PG03 13403.334717 8578.914580 21202.358406 999999.999999'} {'PG04 21419.375320 3884.114643 15262.695510 999999.999999'} {'PG05 -5444.358465 -23231.735097 -11622.918269 999999.999999'} {'PG06 4256.078046 -15022.225421 21541.035346 999999.999999'} {'PG07 20999.600142 2530.514111 -16014.701188 999999.999999'} {'PG08 15137.004373 8787.943441 -20067.712986 999999.999999'} {'PG09 25252.573960 -5495.030484 6117.565428 999999.999999'} {'PG10 -11649.338578 20299.481216 -12184.853410 999999.999999'} {'PG11 23869.410476 9609.902042 -6213.835624 999999.999999'} {'PG12 -11078.110511 -12908.830279 20186.984900 999999.999999'} {'PG13 -5069.695448 -14295.040754 -21942.034600 999999.999999'} {'PG14 13195.364360 -19099.425641 -12902.121600 999999.999999'} {'PG15 -16577.988805 -7914.716508 -19652.160060 999999.999999'} {'PG16 586.618428 24565.416929 -9699.637899 999999.999999'} {'PG17 20043.586102 -13941.848719 10878.356026 999999.999999'} {'PG18 -18787.080782 1044.955503 -18775.154815 999999.999999'} {'PG19 13630.274976 -15179.007750 16668.018520 999999.999999'} {'PG20 -15641.350327 3023.484882 -21226.355026 999999.999999'} {'PG21 21034.035993 16391.252968 -2046.008909 999999.999999'} {'PG22 11292.732738 17609.187114 16664.401322 999999.999999'} {'PG23 -14970.916568 10229.210383 -19413.158370 999999.999999'} {'PG24 -21493.499333 -15954.496427 -966.614535 999999.999999'} {'PG25 -15374.507127 2314.959063 21237.161807 999999.999999'} {'PG26 -3813.654456 26112.998712 1087.903689 999999.999999'} {'PG27 2420.556769 14956.920079 -21951.429167 999999.999999'} {'PG28 12450.986134 -21051.052319 -10030.236278 999999.999999'} {'PG29 -25700.119870 4001.500686 5441.895674 999999.999999'} {'PG30 12632.605806 -9705.387540 -21220.741610 999999.999999'}
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 Comments
Rik
Rik on 6 Jul 2021
Edited: Rik on 6 Jul 2021
Get my readfile function from the FEX (see the link in the answer). You can also use the AddOn manager to install it. This code will work all the way back to Matlab 6.5, so R2019a is no problem.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!