Filter löschen
Filter löschen

How can extract specific numbers from a string???

1 Ansicht (letzte 30 Tage)
Thimiod Athan
Thimiod Athan am 9 Sep. 2016
Kommentiert: Thimiod Athan am 9 Sep. 2016
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!!!

Akzeptierte Antwort

Guillaume
Guillaume am 9 Sep. 2016
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'.

Weitere Antworten (1)

Image Analyst
Image Analyst am 9 Sep. 2016
Have you tried sscanf() or str2double()?
  1 Kommentar
Thimiod Athan
Thimiod Athan am 9 Sep. 2016
Bearbeitet: Thimiod Athan am 9 Sep. 2016
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by