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

3 vues (au cours des 30 derniers jours)
Pierre
Pierre le 3 Fév 2025
Commenté : Pierre le 5 Fév 2025
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 commentaires
Walter Roberson
Walter Roberson le 3 Fév 2025
Use any of the techniques indicated by @Stephen23
Pierre
Pierre le 5 Fév 2025
Thank you

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 3 Fév 2025
Modifié(e) : Stephen23 le 3 Fév 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'}

Plus de réponses (1)

Matt J
Matt J le 3 Fév 2025
Modifié(e) : Matt J le 3 Fév 2025
Y=[0 4 6];
X=Y+"";
X={X{:}}
X = 1x3 cell array
{'0'} {'4'} {'6'}
  2 commentaires
Pierre
Pierre le 3 Fév 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 le 3 Fév 2025
Modifié(e) : Matt J le 3 Fév 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by