subtracting a number from string

8 ビュー (過去 30 日間)
moaad abd allatif
moaad abd allatif 2019 年 5 月 21 日
コメント済み: Star Strider 2019 年 5 月 21 日
how i can subtract a number from string ?
for example :
subject1_EO - i want to extract only number one..
  1 件のコメント
KSSV
KSSV 2019 年 5 月 21 日
how i can subtract a number from string ? how?

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

回答 (2 件)

Star Strider
Star Strider 2019 年 5 月 21 日
One approach:
str = 'subject1_EO';
nrc = regexp(str, '\d+', 'match')
nr = str2double(nrc{:})
producing:
nrc =
1×1 cell array
{'1'}
nr =
1
  2 件のコメント
Stephen23
Stephen23 2019 年 5 月 21 日
+1 a cunning use of a comma-separated list combined with the demand-driven output of nested functions. Explicit indexing is clearer though:
nr = str2double(nrc{1})
Star Strider
Star Strider 2019 年 5 月 21 日
Thank you!

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


Stephen23
Stephen23 2019 年 5 月 21 日
編集済み: Stephen23 2019 年 5 月 21 日
The most efficient solution by far (and simple too!):
>> str = 'subject1_EO';
>> val = sscanf(str,'subject%f')
val = 1

カテゴリ

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