Sounak
Sounak

Reputation: 4968

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?

Upvotes: 1

Views: 959

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

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.

Upvotes: 1

Jeremy Mangas
Jeremy Mangas

Reputation: 371

I believe that the syntax you want is the following:

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

Upvotes: 2

Related Questions