How to count how many numbers there are in a string?

1 visualización (últimos 30 días)
steve
steve el 11 de En. de 2015
Editada: steve el 21 de En. de 2015
Hi,
Basically I have in a script:
I need Matlab to say '3' in this example.
Any ideas?
  2 comentarios
Guillaume
Guillaume el 11 de En. de 2015
What is the definition of 'a number'? Are 123.456, 1e7, 4*i numbers? Or is it just integers?
Stephen23
Stephen23 el 12 de En. de 2015
Editada: Stephen23 el 12 de En. de 2015
Here are some different ways of writing numbers: do we need to detect them all?:
  • 3
  • 3.14159
  • +3
  • 1.23E-16
  • 0XF3C
  • 001110101
  • Inf
  • two
  • 4/3
  • 75%

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 11 de En. de 2015
This works:
n = length(strsplit(st(~isletter(st))))-1;

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 11 de En. de 2015
str='a b c 45 3 2'
out=numel(regexp(str,'\d+','match'))
  3 comentarios
steve
steve el 20 de En. de 2015
thanks for the answer. how can I explain what it means? I've never seen any of those functions and only numel and redexp come up in the help
Ced
Ced el 20 de En. de 2015
That's because these are the only two functions there. You can find a list and description of the different input arguments of e.g. regexp in the documentation: Regexp Documentation

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by