how to remove values based on space delimiter

2 visualizaciones (últimos 30 días)
Sharen H
Sharen H el 24 de Dic. de 2012
the values present in my file is
f 1 2 3
f 1 2 3
f 1 2 3 4
f 2 3 4
f 4 5 6 7
i have to read every line and check for 4 values separated by space
ie f 1 2 3
if that line has 5 values the last value should get removed
ie
my output should be
f 1 2 3
f 1 2 3
f 1 2 3
f 2 3 4
f 4 5 6
does any built in is there ..pls help....thanks in advance

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Dic. de 2012
S = fileread('YourFileName.txt'); %read file
NewS = regexprep(S, '(?m)(?-s)^(\w+\s+\w+\s+\w+).*', '$1'); %do edit
fid = fopen('YourNewFile.txt, 'wt'); %write out new version
fwrite(fid, NewS);
fclose(fid);
  3 comentarios
Sharen H
Sharen H el 24 de Dic. de 2012
if the values are like this
f 1\2 2\3 2\4 4\5
what changes should i ,make
Walter Roberson
Walter Roberson el 24 de Dic. de 2012
'(?m)(?-s)^(\w+\s+[0-9\\]+\s+[0-9\\]+).*'
This presumes that the first thing on the line will not be of the same form, and that the other values only have digits and backslash.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by