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!