Delete the string symbols on specific line with special symbol.

조회 수: 4 (최근 30일)
Long Hà Vinh
Long Hà Vinh 2018년 12월 11일
댓글: Adam Danz 2018년 12월 13일
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.

채택된 답변

Adam Danz
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
Long Hà Vinh
Long Hà Vinh 2018년 12월 12일
@Adam: Error fixed. Thank you!
Adam Danz
Adam Danz 2018년 12월 12일
Curious what your solution was. Mind sharing it here?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Long Hà Vinh
Long Hà Vinh 2018년 12월 13일
@Adam: Just a little bit modify your code bro :) :)
close all; clear all; clc;
%txtFile = 'C:\Users\name\Documents\MATLAB\mlc.txt';
txtFile='input'
% 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\n|', 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',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';
newFile='output'
fid = fopen(newFile, 'wt');
fprintf(fid, [txtCell{:}]);
fclose(fid);
as you see. I only remove "/n" at line 13 and add "/n" at line 11.
Cheer!

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by