extracting all lines includes specific string from text file

조회 수: 18 (최근 30일)
sermet OGUTCU
sermet OGUTCU 2021년 7월 6일
댓글: sermet OGUTCU 2021년 7월 6일
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

채택된 답변

Rik
Rik 2021년 7월 6일
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
Rik
Rik 2021년 7월 6일
편집: Rik 2021년 7월 6일
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.
sermet OGUTCU
sermet OGUTCU 2021년 7월 6일
Thank you very much for the solution.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by