1
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?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Sounak
  • 4,803
  • 7
  • 30
  • 48

2 Answers2

2

I believe that the syntax you want is the following:

data = {};
data{1} = 'hello';
Andy
  • 49,085
  • 60
  • 166
  • 233
Jeremy Mangas
  • 361
  • 1
  • 8
1

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.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147