Delete the string symbols on specific line with special symbol.
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi I have file fomated like that.
# 1996 3 4 9 58 11.0 20.2810 106.3000 20.8 2.7 0.0 0.0 0.5 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
# 1996 3 29 10 34 43.4 20.8820 107.2830 28.5 2.6 0.0 0.0 0.7 2
PLV 12.20 1.00 Pg
PLV 20.60 0.75 Sg
Now I need to remove the number on line have symbol "#" from
# 1996 3 4 9 58 11.0 20.2810 106.3000 20.8 2.7 0.0 0.0 0.5 1
to
# 1
Mean that all so thing after "#" to last number need to be remove (between # and number have 4 <space>). Final I need the file like that
# 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
Any one can help me! I try few way but it remove all or change the string only.
댓글 수: 0
채택된 답변
Adam Danz
2018년 12월 11일
편집: Adam Danz
2018년 12월 11일
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';
% Read file and store each line in a cell
txt = fileread(txtFile);
txtCell = strsplit(txt, newline)';
% Find rows that start with '# ####'
hasPattern = regexp(txtCell, '# \d+'); %this searches for "#' followed by 1 space and at least 1 number.
rowIdx = find(~cellfun(@isempty, hasPattern)); %row numbers that will be replaced
% Replace the rows with new text
newText = strsplit(sprintf('# %d|', 1:length(rowIdx)), '|'); %Here's where you create the new text; ignore last element.
txtCell(rowIdx) = newText(1:end-1);
txtCell = cellfun(@(x)sprintf('%s\n',x), txtCell, 'UniformOutput', false); %add 'newrow' char
% Write text to new file (or you could overwrite the old one).
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
댓글 수: 3
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!