Eastsun
Eastsun

Reputation: 18859

How to convert a string array to a cell array of string?

I have a string array:

array = ['123';'abc';'uvw'];

I want convert it to a cell array of string:

cellArr = {'123';'abc';'uvw'};

There are two ways in my idea: allocating a cell array then using a for loop or

cellArr = arrayfun(@(x) array(x,:),1:size(array,1),'UniformOutput',false)';

But I'm not sure if there is a build-in function do this more effective.

Upvotes: 1

Views: 7191

Answers (1)

carandraug
carandraug

Reputation: 13081

Use cellstr(). Simply cellstr (array) should work. It also works in Octave.

Alternatively, a more complicated but fun way to look into is

mat2cell (array, ones (size (array, 1), 1), size (array, 2))

which in Octave can be

mat2cell (array, ones (rows (array), 1), columns (array))

Upvotes: 2

Related Questions