Here we use fileread() to read your txt file into matlab and then we put each line of text into a cell array. Using regexp() we find which lines start with the pattern "# ****" where the asterisks are numbers. Then we replace those lines with your new pattern: "# *" and write it to a new text file using fprintf().
txtFile = 'C:\Users\name\Documents\MATLAB\mlc.txt';
txt = fileread(txtFile);
txtCell = strsplit(txt, newline)';
hasPattern = regexp(txtCell, '# \d+');
rowIdx = find(~cellfun(@isempty, hasPattern));
newText = strsplit(sprintf('# %d|', 1:length(rowIdx)), '|');
txtCell(rowIdx) = newText(1:end-1);
txtCell = cellfun(@(x)sprintf('%s\n',x), txtCell, 'UniformOutput', false);
newFile = 'C:\Users\name\Documents\MATLAB\mlcNEW.txt';
fid = fopen(newFile, 'wt');
fprintf(fid, [txtCell{:}]);
fclose(fid);
Your new txt file will look like this
# 1
PLV 12.20 1.00 Pg
PLV 21.00 0.75 Sg
HNV 16.20 1.00 Pg
HNV 28.40 0.75 Sg
# 2
PLV 12.20 1.00 Pg
PLV 20.60 0.75 Sg
Best Answer