Extracting number from a string

조회 수: 422 (최근 30일)
Anh Tran
Anh Tran 2016년 12월 16일
편집: Stephen23 2022년 11월 3일
How can I extract the number 1 from the string 'M1' ?

채택된 답변

Stephen23
Stephen23 2016년 12월 16일
Here are two very simple methods:
>> str = 'M1';
>> sscanf(str,'M%d')
ans = 1
>> str(2)-'0'
ans = 1
  댓글 수: 2
Anh Tran
Anh Tran 2016년 12월 16일
Thanks for the great answer! But can you also explain the code for me?
Stephen23
Stephen23 2016년 12월 17일
sscanf reads the numeric values in a string. Read its documentation to know how it works.
The second method subtracts the character code for the character '0' from the second character of the string. This is a simple way of converting digits into a vector of values.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

José-Luis
José-Luis 2016년 12월 16일
편집: José-Luis 2016년 12월 16일
result = 'M1' - '0';
result(result < 0 | result > 9) = [];
  댓글 수: 1
Anh Tran
Anh Tran 2016년 12월 16일
Thanks for the great answer! But can you also explain the code for me?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 12월 16일
Here are two ways, depending on if you want the extracted "1" to be a character (string) or a number:
str = 'M1' % First define the string.
str1 = str(2) % Gives a character
dbl1 = str2double(str(2)) % Give a number (double)
  댓글 수: 1
Anh Tran
Anh Tran 2016년 12월 16일
Thanks! That's a great explanation.

댓글을 달려면 로그인하십시오.


Pierre Harouimi
Pierre Harouimi 2022년 11월 2일
You can use pattern from R2020b:
numbers = str2double(extract(string, digitsPattern));
  댓글 수: 2
Image Analyst
Image Analyst 2022년 11월 2일
편집: Image Analyst 2022년 11월 2일
Example:
string = 'abc123def456789ghi'
string = 'abc123def456789ghi'
numbers = str2double(extract(string, digitsPattern))
numbers = 2×1
123 456789
It would be nice if they made a little wrapper function for that, since it's hard to remember. Something simple like
numbers = extractNumbers(string);
would be easier for most people I think, than having to know about digitsPattern, extract, str2double, and then having to nest them all together.
Stephen23
Stephen23 2022년 11월 3일
편집: Stephen23 2022년 11월 3일
"Something simple like... would be easier for most people I think..."
The devil is in the details, because it really depends on what you define as a "number". Some users will want to match fractional digits (which probably means selecting for e.g. comma vs dot vs comma|dot), whereas others will want to match only integers and to treat commas/dots as non-numeric. Ditto exponent notation, NaNs, Infs, other bases, fixed-width numbers, various Unicode minus/plus signs, etc.
So it turns out to be not very simple at all, once the different "number" formats are considered. The user specifying a regular expression or MATLAB pattern is one way to give them that control, by making those choices explicit.

댓글을 달려면 로그인하십시오.

카테고리

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