how to execute str2num or str2double in matlab?
이전 댓글 표시
I am having trouble using commands str2num.
I have a character
t='2'
'u1'
'u2 '
I used the command p=str2double(char(t)). Right now if I use str2num, a null array appears in the output whereas using str2double gives NaN in output. While searching about these I learned that these character values are not converted to numbers.
I wanted p to store values [2, 1, 2]. How can I do that? Thanks for reading...
댓글 수: 4
Walter Roberson
2018년 5월 9일
Is t a char vector that might at different times have '2' or 'u1' or 'u2 ' ? Or is t a cell array of character vectors that has those three character vectors in it? Or is t a single character vector '2 u1 u2 ' ?
In the number portion, is it necessary to recognize negative values? Is it necessary to recognize decimal points and fractions? How about exponential numbers? How about complex values?
MEENAL SINGHAL
2018년 5월 9일
편집: per isakson
2018년 5월 9일
per isakson
2018년 5월 9일
편집: per isakson
2018년 5월 9일
Hint:
t = { '2' 'u1' 'u2 '};
c = regexp( t, '[\d ]+', 'match' );
p = cellfun( @str2double, c )
outputs
p =
2 1 2
MEENAL SINGHAL
2018년 5월 9일
편집: MEENAL SINGHAL
2018년 5월 9일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!