Split a char without spaces and arbitrarily characters

조회 수: 4 (최근 30일)
Michael Eissler
Michael Eissler 2022년 11월 16일
댓글: Michael Eissler 2022년 11월 16일
Hello together,
I need your help to split my text. As you can see, I have a char with the name "text" (in my project i will read this row from a textfile).
Now I have to split this row into 13 parts. Sometimes a space is in between, sometimes not. I have problems to split the parts, when exponential notation is given together.
My consideration was to split the text with strsplit after each space AND after the character "E***". The * stands for any characters after the E.
For example, the first 4 cells should be like this: 17575 3.10705E+00 -7.07230E+01 -1.77433E-01.
Thansk for your help!
Mcihael
text = sprintf(' 17575 3.10705E+00-7.07230E+01-1.77433E-01-2.72479E-05-7.19082E-06-9.65426E-06 1.70247E-04 4.89104E-05 3.56048E-05 3.71071E+01-3.38230E+01-1.77433E-01');

채택된 답변

Stephen23
Stephen23 2022년 11월 16일
편집: Stephen23 2022년 11월 16일
format long G
txt = ' 17575 3.10705E+00-7.07230E+01-1.77433E-01-2.72479E-05-7.19082E-06-9.65426E-06 1.70247E-04 4.89104E-05 3.56048E-05 3.71071E+01-3.38230E+01-1.77433E-01';
vec = sscanf(txt,'%f') % the simplest and most efficient approach is to convert to numeric
vec = 13×1
1.0e+00 * 17575 3.10705 -70.723 -0.177433 -2.72479e-05 -7.19082e-06 -9.65426e-06 0.000170247 4.89104e-05 3.56048e-05
rgx = '\s*[-+]?\d+\.?\d*(E[-+]\d+)?';
spl = regexp(txt,rgx,'match') % if you really want to fiddle around with text
spl = 1×13 cell array
{' 17575'} {' 3.10705E+00'} {'-7.07230E+01'} {'-1.77433E-01'} {'-2.72479E-05'} {'-7.19082E-06'} {'-9.65426E-06'} {' 1.70247E-04'} {' 4.89104E-05'} {' 3.56048E-05'} {' 3.71071E+01'} {'-3.38230E+01'} {'-1.77433E-01'}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by