Store only numbers from user input

1 回表示 (過去 30 日間)
Raldi
Raldi 2011 年 12 月 1 日
Hi guys
Is there a way to store only the numbers from lets say an input of the type S^2+67*s+98

採用された回答

Walter Roberson
Walter Roberson 2011 年 12 月 1 日
numstrs = regexp(TheString, '-?\d+', 'match');
then convert each of the cell strings returned in numstrs to numeric form. The -? part is to recognize an optional negative sign.
If you want to be able to accept floating point numbers, or if you want to recognize fractions as being numbers, or if you want to recognize pi as being a number, then the task gets more difficult.
  1 件のコメント
Raldi
Raldi 2011 年 12 月 1 日
No i think this will do, thank you Walter.

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

その他の回答 (1 件)

Jan
Jan 2011 年 12 月 1 日
Another approach from the times of Matlab 5.3 without REGEXP:
S = 'S^2+67*s+98';
S((S < '0' | S > '9') & S ~= '.') = ' ';
N = sscanf(S, '%g');
  3 件のコメント
Raldi
Raldi 2011 年 12 月 1 日
Seems that i have another problem now. What if i only need the numbers that are lets say just before the letter S. I tried to figure it out from the matlab's help but with no luck.
Walter Roberson
Walter Roberson 2011 年 12 月 1 日
Answered in your newer question.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by