Sum of matrix elements after using regexp

조회 수: 1 (최근 30일)
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 1월 24일
답변: Star Strider 2018년 1월 24일
I wrote the following code to sum all number after locating numeric values from a string by using regexp.
s='2 groupd 40cell f'
g =regexp(s,'\d*','match')
g = cell2mat(g)
It gives an incorrect result: 240 --> instead of [2 40]

채택된 답변

Star Strider
Star Strider 2018년 1월 24일
The numbers are strings, so cell2mat will concatenate them into one string array.
The solution is to use the str2double function if you want them as separate numbers:
g = str2double(g)
g =
2 40

추가 답변 (0개)

카테고리

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