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?

 채택된 답변

Geoff Hayes
Geoff Hayes 2019년 1월 4일
편집: Geoff Hayes 2019년 1월 4일

2 개 추천

Richard - from strsplit try doing the following
strsplit(data, ';', 'CollapseDelimiters', false)
so that the consecutive empty delimiters are not collapsed into one cell.

추가 답변 (2개)

madhan ravi
madhan ravi 2019년 1월 4일
편집: madhan ravi 2019년 1월 7일

1 개 추천

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

Jan
Jan 2019년 1월 7일
편집: Jan 2019년 1월 7일
+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 2019년 1월 7일
Echt toll Jan! Danke schön , selbsverständlich jetzt..

댓글을 달려면 로그인하십시오.

Richard Youden
Richard Youden 2019년 1월 7일

0 개 추천

Thanks for the responses, greatly appreciated.
Richard

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

릴리스

R2015b

태그

질문:

2019년 1월 4일

편집:

2019년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by