Reputation: 363
I have a column vector of integers and I want to convert it into cell in matlab. The following is the code. But it did not output what I expected.
nodes = [10; 21; 44];
nodes = num2str(nodes)
nodes = num2cell(nodes)
nodes =
10
21
44
nodes =
'1' '0'
'2' '1'
'4' '4'
Can anyone help me fix this? Many thanks for your time and attention.
Upvotes: 0
Views: 1227
Reputation: 45741
Another way is to use arrayfun
(i.e. a wrapper for a for-loop):
arrayfun(@num2str, nodes, 'uni', 0)
or if you want numbers in the cells (not strings) then it's just
num2cell(nodes)
i.e. without the num2str
Upvotes: 0
Reputation: 13923
In the third line's argument, nodes
is a string and not a number anymore.
Therefore you can use the cellstr
-function to convert the string-array to a cell-array of strings.
nodes = [10; 21; 44];
nodes = num2str(nodes)
nodes = cellstr(nodes)
This outputs:
nodes =
10
21
44
nodes =
'10'
'21'
'44'
Upvotes: 1