Using sscanf to extract numbers from string

15 vues (au cours des 30 derniers jours)
Katelyn
Katelyn le 16 Juin 2016
Hi everyone, I'm having some trouble with sscanf, I'm trying to read two sets of number from a string which looks something like this 30cycles_sample2_11_29.csv. I want to extract the last two numbers, separated by underscores. Since the format of the string won't change I'd like to just read all numbers from the string, and can separate out the ones I'd like after. I've tried sscanf('30cycles_sample2_11_29.csv','%f') and variations but can only ever read the first number, 30 in this case. Any help is appreciated!

Réponse acceptée

Star Strider
Star Strider le 16 Juin 2016
This works for me:
str = '30cycles_sample2_11_29.csv';
out = sscanf(str, '30cycles_sample2_%2d_%2d.csv')
out =
11
29
  4 commentaires
Learner
Learner le 12 Nov 2019
Modifié(e) : Learner le 12 Nov 2019
Can we extract float values??
Gabriel Barros
Gabriel Barros le 8 Jan 2020
%f instead of %d, i guess

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