Borrar filtros
Borrar filtros

Help splitting string using strsplit

3 visualizaciones (últimos 30 días)
Richard Youden
Richard Youden el 4 de En. de 2019
Editada: madhan ravi el 7 de En. de 2019
I have a large set of data which is presented as a string, for example:
1;2;3;4;5;6;7;8 etc...
I use:
strsplit(data,';')
and that breaks everything up into individual cells.
However I have found an issue with some data. My logger is 100% reliable so sometimes I get data which looks like:
;;;;;6;7;8 etc...
Using the strsplit command I get:
NaN 6 7 8
What I'd like is
NaN NaN NaN NaN NaN 6 7 8
Any suggestions?

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 4 de En. de 2019
Editada: Geoff Hayes el 4 de En. de 2019
Richard - from strsplit try doing the following
strsplit(data, ';', 'CollapseDelimiters', false)
so that the consecutive empty delimiters are not collapsed into one cell.

Más respuestas (2)

madhan ravi
madhan ravi el 4 de En. de 2019
Editada: madhan ravi el 7 de En. de 2019
str=';;;;;6;7;8';
expr=';';
C=str2double(regexp(str,expr,'split')) % edited after Jan’s comment
%[~,c]=regexp(str,expr,'match','split');
%Result=cellfun(@str2double,c)
Gives:
Result =
NaN NaN NaN NaN NaN 6 7 8
  2 comentarios
Jan
Jan el 7 de En. de 2019
Editada: Jan el 7 de En. de 2019
+1. regexp returns the wanted cell string as 1st output when using 'split' without 'match':
C = regexp(str, expr, 'split')
and str2double works with a cellstring directly. So this is simpler and faster:
Result = str2double(regexp(str, expr, 'split'))
madhan ravi
madhan ravi el 7 de En. de 2019
Echt toll Jan! Danke schön , selbsverständlich jetzt..

Iniciar sesión para comentar.


Richard Youden
Richard Youden el 7 de En. de 2019
Thanks for the responses, greatly appreciated.
Richard

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by