how to convert num to string ?

조회 수: 4 (최근 30일)
Jan Risn
Jan Risn 2019년 4월 30일
댓글: Jan Risn 2019년 4월 30일
I have data in one column as follows
x =
[22]
'22 .8 '
[30]
'39 .6 '
[44]
[48]
'49 .6 '
'50 .8 '...
how do I convert to a string?
I have tried using str2double (x) but it displays the results
Nan
22.8000
Nan
39.6000
Nan
Nan
49.6000
50.8000
I am confused when in one data there are 2 different data, namely ('x' and [x])..
I am very grateful to those who have been kind enough to help me
Regards
Jan

채택된 답변

KSSV
KSSV 2019년 4월 30일
x = { [22]
'22 .8 '
[30]
'39 .6 '
[44]
[48]
'49 .6 '
'50 .8 '} ;
idx = cellfun(@ischar,x) ;
y = cellfun(@num2str,x(~idx),'un',0)
x(~idx) = y ;
x
  댓글 수: 1
Jan Risn
Jan Risn 2019년 4월 30일
this works, thank you for your help

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 4월 30일
x ={...
[22]
'22 .8 '
[30]
'39 .6 '
[44]
[48]
'49 .6 '
'50 .8 '};
lo = cellfun(@ischar,x);
out = zeros(size(x));
out(~lo) = cell2mat(x(~lo));
out(lo) = str2double(regexprep(x(lo),'\s',''));
  댓글 수: 1
Jan Risn
Jan Risn 2019년 4월 30일
thank you for your help, this really helped me

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

카테고리

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