How can extract specific numbers from a string???

I have the string c={ 4 of 5 stars, 5 of 5 stars, 5 of 5 stars, 3 of 5 stars, 3 of 5 stars, 4 of 5 stars}. How can extract to a matrix the bold numbers ????
thank you!!!

 채택된 답변

Guillaume
Guillaume 2016년 9월 9일

0 개 추천

You could use a regular expression. Assuming all the numbers you want to extract are integer:
str = 'c={ 4 of 5 stars, 5 of 5 stars, 5 of 5 stars, 3 of 5 stars, 3 of 5 stars, 4 of 5 stars}';
starcount = str2double(regexp(str, '\d+(?= of)', 'match'))
The regular expression matches any sequence of digit immediately followed by ' of'.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 9월 9일

0 개 추천

Have you tried sscanf() or str2double()?

댓글 수: 1

Thimiod Athan
Thimiod Athan 2016년 9월 9일
편집: Thimiod Athan 2016년 9월 9일
Yes but I want to extract only the bold number not all the numbers. one idea is Value = sscanf(a(1), '%g', 1) Value1 = sscanf(a(14), '%g', 1) Value2 = sscanf(a(27), '%g', 1)....etc

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2016년 9월 9일

댓글:

2016년 9월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by