MATLAB Answers


How can I read a string into a cell array?

Asked by Eric
on 15 Apr 2013

I want to read an ascii file into a cell array of strings, one cell per line of input text:

array = cell(10000);
iLine = 1;
fid1  = fopen('template.txt','r');
while ~feof(fid1)
     line = fgetl(fid1);
     array(iLine) = line;
     iLine = iLine + 1

When I do the above, I get the following error: Conversion to cell from char is not possible.

I've read through answers both here and on the Web and tried many things. But I am a Matlab newbie and I think I am misunderstanding something fundamental about how to deal with strings. Any suggestions? Thanks.



No products are associated with this question.

2 Answers

Answer by per isakson
on 15 Apr 2013
Edited by per isakson
on 15 Apr 2013
 Accepted answer

fgetl returns a string. Try

    class( line )

line need to be converted to a cell array of strings:

    array(iLine) = { line };

PS. Before other resources, search the Matlab on-line help.

  1 Comment

on 15 Apr 2013

Great, thanks.

Answer by Image Analyst
on 15 Apr 2013

Alternatively this will also work

array{iLine} = line;

This way basically says the "contents of" cell # iLine is a string. per's version does the same thing but in a slightly different way. It puts the string into a single cell and then makes that cell the element number iLine of the full cell array. It's somewhat confusing so let me try to make an analogy. A cell is like a bucket. You can throw anything you want into the bucket: a string, an integer, a double, an array, a structure, even another cell array. Now let's say you have an array of buckets - an array of cells or a "Cell Array". Each bucket can contain something different, or they mgiht all contain the same type of variable. Bucket 1 could contain a string while bucket 2 could contain an image (array of uint8's), while bucket 3 could be a int32. Or all buckets could contain strings of various lengths. It's totally flexible.

The braces should be read as "contents of", so in my way, I said the "contents of" bucket #iLine is a string. per's way says let's get a bucket (a cell) and put the string into it. Then let's take that bucket and make it bucket #iLine, replacing any bucket that was already there. He used parentheses which means it refers to the whole single bucket (the bucket plus the contents) while I used braces which refers to only the contents of the bucket. It's just a slight difference - a slightly different way of considering it. They are equivalent for the most part. I use both ways and I don't really think one way or the other is really preferred. You can use whatever way is easier for you to think about it. Maybe one way will be more intuitive for you than the other way, but again, they are equivalent.

I hope that explains cell arrays better for you.


on 15 Apr 2013

That's very helpful - thanks for the analogy.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!