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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by