extracting all lines includes specific string from text file
조회 수: 18 (최근 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에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!