Delete the string symbols on specific line with special symbol.

8 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Adam Danz
Adam Danz el 11 de Dic. de 2018
Editada: Adam Danz el 11 de Dic. de 2018
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 comentarios
Long Hà Vinh
Long Hà Vinh el 12 de Dic. de 2018
@Adam: Error fixed. Thank you!
Adam Danz
Adam Danz el 12 de Dic. de 2018
Curious what your solution was. Mind sharing it here?

Iniciar sesión para comentar.

Más respuestas (1)

Long Hà Vinh
Long Hà Vinh el 13 de Dic. de 2018
@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!

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by