vector conversion from a vector of numbers to a vector cell of chars.

조회 수: 2 (최근 30일)
Pierre
Pierre 2025년 2월 3일
댓글: Pierre 2025년 2월 5일
Hello,
In MATLAB I have this vector: Y=[0 4 6] and I need to convert it to this format X={'0' '4' '6'}.
Not sure how to do it.
Thank you
  댓글 수: 6

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

채택된 답변

Stephen23
Stephen23 2025년 2월 3일
편집: Stephen23 2025년 2월 3일
Y = [0,4,6];
X = cellstr(string(Y))
X = 1x3 cell array
{'0'} {'4'} {'6'}
X = arrayfun(@num2str,Y,'uni',0)
X = 1x3 cell array
{'0'} {'4'} {'6'}
X = compose('%u',Y(:)).'
X = 1x3 cell array
{'0'} {'4'} {'6'}
X = split(num2str(Y)).'
X = 1x3 cell array
{'0'} {'4'} {'6'}
X = num2cell(char(Y+'0')) % unlikely to be what you want
X = 1x3 cell array
{'0'} {'4'} {'6'}
X = sprintfc('%u',Y) % undocumented
X = 1x3 cell array
{'0'} {'4'} {'6'}

추가 답변 (1개)

Matt J
Matt J 2025년 2월 3일
편집: Matt J 2025년 2월 3일
Y=[0 4 6];
X=Y+"";
X={X{:}}
X = 1x3 cell array
{'0'} {'4'} {'6'}
  댓글 수: 2
Pierre
Pierre 2025년 2월 3일
unless I am missing something this is not what I need.
{'0'} {'4'} {'6'} is not {'0' '4' '6'}.
To be sure I tried it in COMSOL and it crashed
Matt J
Matt J 2025년 2월 3일
편집: Matt J 2025년 2월 3일
They are the same, as you can see at the command line,
>> {'0' '4' '6'}
ans =
1×3 cell array
{'0'} {'4'} {'6'}
So, you will have to review what it is you think you need.

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by