extracting all lines includes specific string from text file
    5 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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 件のコメント
採用された回答
  Rik
      
      
 2021 年 7 月 6 日
        %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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
				Help Center および File Exchange で Characters and Strings についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

