Convert single string with many numbers to vector

조회 수: 32 (최근 30일)
Pati Stan
Pati Stan 2019년 7월 31일
댓글: Stephen23 2019년 8월 7일
I have the following string
hue = '10 20 30 40 50';
That I want to turn into a vector of those 5 values
hue_vec = [10 20 30 40 50];
How can I do this? Thanks in advance!

채택된 답변

madhan ravi
madhan ravi 2019년 7월 31일
hue_vec = str2double(regexp(hue,'\d+','match'))
  댓글 수: 1
madhan ravi
madhan ravi 2019년 7월 31일
If you have decimals then the expressions would be:
hue_vec = str2double(regexp(hue,'\d*[\.]?\d*','match'))

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

추가 답변 (3개)

Fangjun Jiang
Fangjun Jiang 2019년 7월 31일
a=str2num(hue);
  댓글 수: 2
Pati Stan
Pati Stan 2019년 7월 31일
Also works great! Thanks!
Stephen23
Stephen23 2019년 8월 1일
Note that str2num relies on eval.

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


Stephen23
Stephen23 2019년 8월 1일
편집: Stephen23 2019년 8월 1일
Very simple, very efficient, no evil eval:
>> hue = '10 20 30 40 50';
>> vec = sscanf(hue,'%f',[1,Inf])
vec =
10 20 30 40 50

Pati Stan
Pati Stan 2019년 7월 31일
This worked beautifully! Thank you!

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by