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

3 Ansichten (letzte 30 Tage)
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Feb. 2025
Bearbeitet: Stephen23 am 3 Feb. 2025
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'}

Weitere Antworten (1)

Matt J
Matt J am 3 Feb. 2025
Bearbeitet: Matt J am 3 Feb. 2025
Y=[0 4 6];
X=Y+"";
X={X{:}}
X = 1x3 cell array
{'0'} {'4'} {'6'}
  2 Kommentare
Pierre
Pierre am 3 Feb. 2025
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 am 3 Feb. 2025
Bearbeitet: Matt J am 3 Feb. 2025
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by