Split a char without spaces and arbitrarily characters

3 次查看(过去 30 天)
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