Search code examples
arraysstringmatlabcell-array

MATLAB: create a list/cell array of strings


data = {};
data(1) = 'hello';

gives this error Conversion to cell from char is not possible.

my strings are created inside a loop and they are of various lengths. How do I store them in a cell array or list?


Solution

  • Use curly braces to refer to the contents of a cell:

    data{1} = 'hello'; %// assign a string as contents of the cell
    

    The notation data(1) refers to the cell itself, not to its contents. So you could also use (but it's unnecessarily cumbersome here):

    data(1) = {'hello'}; %// assign a cell to a cell
    

    More information about indexing into cell arrays can be found here.