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

1 回表示 (過去 30 日間)
steve
steve 2015 年 1 月 11 日
編集済み: steve 2015 年 1 月 21 日
Hi,
Basically I have in a script:
I need Matlab to say '3' in this example.
Any ideas?
  2 件のコメント
Guillaume
Guillaume 2015 年 1 月 11 日
What is the definition of 'a number'? Are 123.456, 1e7, 4*i numbers? Or is it just integers?
Stephen23
Stephen23 2015 年 1 月 12 日
編集済み: Stephen23 2015 年 1 月 12 日
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%

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2015 年 1 月 11 日
This works:
n = length(strsplit(st(~isletter(st))))-1;

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 1 月 11 日
str='a b c 45 3 2'
out=numel(regexp(str,'\d+','match'))
  3 件のコメント
steve
steve 2015 年 1 月 20 日
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 2015 年 1 月 20 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by