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

3 visualizzazioni (ultimi 30 giorni)
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 Commenti

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 3 Feb 2025
Modificato: Stephen23 il 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'}

Più risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by