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?

4
  • 1
    data{1} = 'hello'. Use curly braces to refer to the content of a cell Commented May 15, 2015 at 16:55
  • @LuisMendo Yes! It works. If you post this as an answer I will accept it. Commented May 15, 2015 at 16:57
  • Done. I've expanded the explanation a little Commented May 15, 2015 at 17:00
  • possible duplicate of How to use cell arrays in Matlab? Commented May 26, 2015 at 8:37

2 Answers 2

2

I believe that the syntax you want is the following:

data = {};
data{1} = 'hello';
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.