Split a char without spaces and arbitrarily characters

3 vues (au cours des 30 derniers jours)
Michael Eissler
Michael Eissler le 16 Nov 2022
Commenté : Michael Eissler le 16 Nov 2022
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');

Réponse acceptée

Stephen23
Stephen23 le 16 Nov 2022
Modifié(e) : Stephen23 le 16 Nov 2022
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'}

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by