Identify a number in a string

6 visualizaciones (últimos 30 días)
Omphemetse Moeng
Omphemetse Moeng el 11 de Oct. de 2020
Comentada: Omphemetse Moeng el 11 de Oct. de 2020
Hi all, I would like to know how to identify and extract a number or digit from a string or text, so if I have Ans = ['2.66 meters in the front of the mirror']; I would only like to identify and extract the double, this should be a general thing and not only specific to the 2.66. Please help

Respuesta aceptada

madhan ravi
madhan ravi el 11 de Oct. de 2020
Wanted = str2double(regexp(Ans, '(+|-)?(\d+)?\.?\d*', 'match'))

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 11 de Oct. de 2020
Editada: Ameer Hamza el 11 de Oct. de 2020
Try sscanf()
str = '2.66 meters in the front of the mirror';
nums = sscanf(str, '%f');
Result
>> nums
nums =
2.6600
  1 comentario
Omphemetse Moeng
Omphemetse Moeng el 11 de Oct. de 2020
Thank you, this only works if the number is placed first, so generally should its position in the string change it does not work anymore, I then get 0x0 double. Is there a more general way to this?

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by