How to extract vectors from strings

4 visualizaciones (últimos 30 días)
KostasK
KostasK el 10 de Nov. de 2021
Editada: Stephen23 el 10 de Nov. de 2021
I have the following two strings from which I would like to extract the numbers in two respective vectors:
A = 'REAL 1.001 2.801 4.378 6.283 7.941 10.774 12.436 14.760 16.205 17.577 18.214 19.581 20.810 22.027' ;
B = '0.0 +3.188E-2 +8.918E-2 +1.394E-1 +2.000E-1 +2.528E-1 +3.430E-1 +3.959E-1 +4.698E-1 +5.159E-1 +5.595E-1 +5.798E-1 +6.233E-1 +6.625E-1 +7.012E-1' ;
However, str2num and str2double don't work well in these circumstances as in both cases text is contained within the string either in the first 'element' of the string vector, or as part of the scientific notation.
Also, I have tried sscanf(A, '%f'), however that suprisingly returns an empty vector.
Therefore, how can I extract these numbers from strings A and B ?

Respuesta aceptada

Stephen23
Stephen23 el 10 de Nov. de 2021
Editada: Stephen23 el 10 de Nov. de 2021
>> A = 'REAL 1.001 2.801 4.378 6.283 7.941 10.774 12.436 14.760 16.205 17.577 18.214 19.581 20.810 22.027' ;
>> Av = sscanf(regexprep(A,'^[A-Za-z]+',''),'%f')
Av =
1.0010
2.8010
4.3780
6.2830
7.9410
10.7740
12.4360
14.7600
16.2050
17.5770
18.2140
19.5810
20.8100
22.0270
>> B = '0.0 +3.188E-2 +8.918E-2 +1.394E-1 +2.000E-1 +2.528E-1 +3.430E-1 +3.959E-1 +4.698E-1 +5.159E-1 +5.595E-1 +5.798E-1 +6.233E-1 +6.625E-1 +7.012E-1';
>> Bv = sscanf(B,'%f')
Bv =
0
0.0319
0.0892
0.1394
0.2000
0.2528
0.3430
0.3959
0.4698
0.5159
0.5595
0.5798
0.6233
0.6625
0.7012

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by