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

59 ビュー (過去 30 日間)
Pierre
Pierre 2025 年 2 月 3 日 17:43
コメント済み: Pierre 2025 年 2 月 5 日 8:12
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 件のコメント
Walter Roberson
Walter Roberson 2025 年 2 月 3 日 22:52
Use any of the techniques indicated by @Stephen23
Pierre
Pierre 2025 年 2 月 5 日 8:12
Thank you

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2025 年 2 月 3 日 17:53
編集済み: Stephen23 2025 年 2 月 3 日 17:59
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 日 22:40
編集済み: Matt J 2025 年 2 月 3 日 22:50
Y=[0 4 6];
X=Y+"";
X={X{:}}
X = 1x3 cell array
{'0'} {'4'} {'6'}
  2 件のコメント
Pierre
Pierre 2025 年 2 月 3 日 23:08
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 日 23:25
編集済み: Matt J 2025 年 2 月 3 日 23:25
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 Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by