how to remove values based on space delimiter
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
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
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Import and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!