Delete the string symbols on specific line with special symbol.

3 ビュー (過去 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 Center および File ExchangeData 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