regexp split at first whitespace

42 visualizaciones (últimos 30 días)
Vincent Scalfani
Vincent Scalfani el 14 de Jul. de 2018
Comentada: Simon el 25 de Abr. de 2023
I have a cell dataset like this, where it is a string of letters and numbers all in one column (many rows):
data =
'0R 2 2 0'
'1R 2 0 0 4'
'2R 2 2 0 1 1 1'
'3R 2 2 2 1 1'
I would like to split each row into 2 columns at the first space:
data =
'0R' '2 2 0'
'1R' '2 0 0 4'
'2R' '2 2 0 1 1 1'
'3R' '2 2 2 1 1'
I tried:
splitcells = regexp(data, '\s+','split');
but that splits at each space, creating numerous columns, not 2. How do I get the regular expression to split only at the first space? Thanks

Respuesta aceptada

Paolo
Paolo el 14 de Jul. de 2018
You may use the once option to split only once at the first occurrence of whitespace.
splitcells = regexp(data,'\s','split','once')
  2 comentarios
Vincent Scalfani
Vincent Scalfani el 14 de Jul. de 2018
Thanks so much! If anyone else has this question, you can use the following to unnest the 1x2 cells from the split:
data = vertcat(data{:});
Simon
Simon el 25 de Abr. de 2023
I have the same problem. Thanks for the answers!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by