How to extract values from a string.

16 vues (au cours des 30 derniers jours)
Mario Verheijen Verheijen
Mario Verheijen Verheijen le 24 Mai 2019
Modifié(e) : madhan ravi le 24 Mai 2019
So I have a string in the following format:
filename = "Delft_2_220_20_4344-5088.csv" ;
And I want to extract the numbers from it, what is a good way to do this?
So the result is something like this:
a=2; b=220; c=20;d=[4344 5088];
  1 commentaire
KSSV
KSSV le 24 Mai 2019
Read about regexp

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 24 Mai 2019
Modifié(e) : Stephen23 le 24 Mai 2019
>> S = 'Delft_2_220_20_4344-5088.csv';
>> V = str2double(regexp(S,'\d+','match'))
V =
2 220 20 4344 5088
Using indexing to allocate those values to whatever other variables you want.
  2 commentaires
madhan ravi
madhan ravi le 24 Mai 2019
Modifié(e) : madhan ravi le 24 Mai 2019
+1 Stephen, also if the string contains decimals then
regexp(s,'\d+[\.]?\d*','match')
Mario Verheijen Verheijen
Mario Verheijen Verheijen le 24 Mai 2019
Thanks Stephen, works perfectly!

Connectez-vous pour commenter.

Plus de réponses (0)

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