How do I use MATLAB to delete specific lines/text from a .txt file

86 views (last 30 days)
I have a text file which is describing a chemical kinetic mechanism, I want to clean the up the file so I can use it in some FORTRAN computing.
I want to delete the lines beginning with ! or REV
How do I go about specifiying lines to delete or specific data ?
I've attached the text file I want to format

Accepted Answer

KSSV
KSSV on 8 Feb 2019
Edited: KSSV on 8 Feb 2019
% Read the file data
fid = fopen('chem679.inp.txt','rt') ;
S = textscan(fid,'%s','delimiter','\n') ;
S = S{1} ;
fclose(fid) ;
% Get the indices of REV and remove them
idx = contains(S,'REV') ;
S(idx) = [] ;
% Get the indices of ! and remove them
idx = contains(S,'!R') ;
S(idx) = [] ;
% write to file
fid = fopen('test.txt','wt') ;
fprintf(fid,'%s\n',S{:});
fclose(fid) ;
  3 Comments
KSSV
KSSV on 8 Feb 2019
ohh......so ! should be checked at only first column....how about now? Edited the code.

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!