enter a string of characters and numbers, add and return the sum of the numbers in matlab, for example: 'there are two blue birds and 4 red birds on the branch'--> return 6

조회 수: 4 (최근 30일)
please help me solve this problem: enter a string of characters and numbers, add and return the sum of the numbers in matlab, for example: 'there are two blue birds and 4 red birds on the branch'--> return 6
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 9월 3일
Which names of numbers need to be handled? For example does it need to be able to handle,
there are seventeen thousand million three hundred and sixty two thousand five hundred and nine red birds
Phung Khai
Phung Khai 2021년 9월 3일
I'm sorry to have entered the wrong topic, the correct topic is: " 2 blue birds and 4 red birds " --> output = 6. need to handle numeric types, like the above example :2 and 4

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

답변 (1개)

Konrad
Konrad 2021년 9월 3일
Hi
if you only need to handle numeric type, I'd recommend regular expressions:
strng = ' 2 blue birds and 4 red birds';
res_str = regexp(strng,'\d+(\.\d+)?','match');
res_num = str2double(res_str);
res_sum = sum(res_num);
If you also want to handle written numbers as in your initial example
'there are two blue birds and 4 red birds on the branch'
this FEX might by of great help.
Best, Konrad
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 9월 3일
The regexp needs a bit of adjustment if you want to handle numbers with leading period such as .75

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by