Using sscanf to extract numbers from string

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!

 Respuesta aceptada

Star Strider
Star Strider el 16 de Jun. de 2016
This works for me:
str = '30cycles_sample2_11_29.csv';
out = sscanf(str, '30cycles_sample2_%2d_%2d.csv')
out =
11
29

4 comentarios

Katelyn
Katelyn el 16 de Jun. de 2016
Thank you!
My pleasure!
You can also just use ‘%d’ and leave out the field width:
out = sscanf(str, '30cycles_sample2_%d_%d.csv')
Learner
Learner el 12 de Nov. de 2019
Editada: Learner el 12 de Nov. de 2019
Can we extract float values??
Gabriel Barros
Gabriel Barros el 8 de En. de 2020
%f instead of %d, i guess

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Jun. de 2016

Comentada:

el 8 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by