How to extract only floating numbers from a string

13 vues (au cours des 30 derniers jours)
parmeshwar prasad
parmeshwar prasad le 20 Juin 2018
Here is my string "21.5VgDC_0.05000V_VgAC_50M-150M30ms47GV1" How can I extract only the numbers 21.5, 0.05000, 50, 150 30 and 47 from the string. Thanks in advance
  1 commentaire
Jan
Jan le 20 Juin 2018
What's about '1e-5', '1D+004', '.1'?

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 20 Juin 2018
>> S = '21.5VgDC_0.05000V_VgAC_50M-150M30ms47GV1';
>> C = regexp(S,'\d+\.?\d*','match');
>> C{:}
ans = 21.5
ans = 0.05000
ans = 50
ans = 150
ans = 30
ans = 47
ans = 1
  3 commentaires
Nadatimuj
Nadatimuj le 9 Mar 2022
Modifié(e) : Nadatimuj le 9 Mar 2022
What if I use C = regexp(S,'\d*\.?\d*','match')?
And what if I use C = regexp(S,'\d*\.?\d+','match')
What is the difference?
Walter Roberson
Walter Roberson le 9 Mar 2022
In the first one everything is optional, so it matches the empty pattern too.
The second one does require at least one digit. However it does not support digits followed by a decimal point with no digits after

Connectez-vous pour commenter.

Plus de réponses (1)

Riadh Essaadali
Riadh Essaadali le 21 Mai 2023
C = regexp(S,'[-+]?\d+\.?\d*','match');

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by