MATLAB: How to remove certain lines of text in a text file

text file

I am trying to remove certain lines of text present in one text file. All these lines needing to be removed are present in another text file I have created. Both are attached. The text file that needs the removal is labeled 'SAMPLEFILE.txt', while the file containing the lines to be removed from 'SAMPLEFILE.txt' is named 'NewGalaxy.txt'.

Best Answer

DataS = fileread('SAMPLEFILE.txt');
% Perhaps you have to fix the linebreaks:
DataS = strrep(DataS, char([13,10]), char(10));
Data = regexp(DataS, char(10), 'split');
PatternS = fileread('NewGalaxy.txt');
PatternS = strrep(PatternS , char([13,10]), char(10));
Pattern = regexp(PatternS , char(10), 'split');
Match = setdiff(Data, Pattern);
% Do you want to create a new file?
fid = fopen('NewFile.txt', 'w');
if fid < 0; error('Cannot open file.'); end
fprintf(fid, '%s\n', Match{:});
fclose(fid);