Loren on the Art of MATLAB

June 21st, 2006

Cell Arrays and Their Contents

I've written several blog articles so far on structures, and not quite so much on their soulmates, cell arrays. Just last week, at the annual MathWorks Aerospace Defense Conference (MADC), I had several people ask for help on cell arrays and indexing. Couple that with the weekly questions on the MATLAB newsgroup, and it's time.

Contents

Arrays

As you probably already know, arrays in MATLAB are rectangular looking in any two dimensions. For example, for each row in a matrix (2-dimensional), there is the same number of elements - all rows have the same number of columns. To denote missing values in floating point arrays, we often use NaN. And each MATLAB array is homogeneous; that is, each array element is the same kind of entity, for example, double precision values.

Cell Arrays

Cell arrays were introduced in MATLAB 5.0 to allow us to collect arrays of different sizes and types. Cell arrays themselves must still be rectangular in any given two dimensions, and since each element is a cell, the array is filled with items that are all the same type. However, the contents of each cell can be any MATLAB array, including

  • numeric arrays, the ones that people typically first learn
  • strings
  • structures
  • cell arrays
clear

Indexing Using Parentheses

Indexing using parentheses means the same thing for all MATLAB arrays. Let's take a look at a numeric array first and then a cell array.

M = magic(3)
M =
     8     1     6
     3     5     7
     4     9     2

Let's place a single element into another array.

s = M(1,2)
s =
     1

Next let's get a row of elements.

row3 = M(3,:)
row3 =
     4     9     2

And now grab the corner elements.

corners = M([1 end],[1 end])
corners =
     8     6
     4     2

What's in the MATLAB workspace?

whos
clear % clean up before we move forward
  Name          Size                    Bytes  Class

  M             3x3                        72  double array
  corners       2x2                        32  double array
  row3          1x3                        24  double array
  s             1x1                         8  double array

Grand total is 17 elements using 136 bytes

Next, let's do similar experiments with a cell array.

C = {magic(3) 17 'fred'; ...
    'AliceBettyCarolDianeEllen' 'yp' 42; ...
    {1} 2 3}
C = 
    [3x3  double]    [17]    'fred'
    [1x25 char  ]    'yp'    [  42]
    {1x1  cell  }    [ 2]    [   3]

Notice the information we get from printing C. We can see it is 3x3, and we can see information, but not necessary full content, about the values in each cell. The very first cell contains a 3x3 array of doubles, the second element in the first row contains the scalar value 17, and the third cell in the first row contains a string, one that is short enough to print out.

Let's place a single element into another array.

sCell = C(1,2)
sCell = 
    [17]

Next let's get a row of elements.

row3Cell = C(3,:)
row3Cell = 
    {1x1 cell}    [2]    [3]

And now grab the corner elements.

cornersCell = C([1 end],[1 end])
cornersCell = 
    [3x3 double]    'fred'
    {1x1 cell  }    [   3]

What's in our workspace now?

whos
clear sCell row3Cell cornersCell
  Name              Size                    Bytes  Class

  C                 3x3                       774  cell array
  cornersCell       2x2                       396  cell array
  row3Cell          1x3                       264  cell array
  sCell             1x1                        68  cell array

Grand total is 84 elements using 1502 bytes

An Observation about Indexing with Parentheses

When we index into an array using parentheses, (), to extract a portion of an array, we get an array of the same type. With the double precision array M, we got double precision arrays of different sizes and shapes as our output. When we do the same thing with our cell array C, we get cell arrays of various shapes and sizes for the output.

Contents of Cell Arrays

Cell arrays are quite useful in a variety of applications. We use them in MATLAB for collecting strings of different lengths. They are good for collecting even numeric arrays of different sizes, e.g., the magic squares from order 3 to 10. But we still need to get information from within given cells, not just create more cell arrays using (). To do so, we use curly braces {}. I used one set of them to create C initially.

Now let's extract the contents from some cells and assign the output to an array.

Let's place a single element into another array.

m = C{1}
m =
     8     1     6
     3     5     7
     4     9     2

Next let's try to get a row of elements.

try
    row3 = C{3,:}
catch
    lerr = lasterror;
    disp(lerr.message(24:end))
end
Illegal right hand side in assignment. Too many elements.

Why couldn't I do that? Let's look at what's in row 1.

C(1,:)
ans = 
    [3x3 double]    [17]    'fred'

Now let's see what we get if we look at the contents without assigned the output to a variable.

C{1,:}
ans =
     8     1     6
     3     5     7
     4     9     2
ans =
    17
ans =
fred

You can see that we assign to ans three times, one for each element in the row of the cell array. It's as if we wrote this expression:

C{1,1},C{1,2},C{1,3}

with the output from these arrays being successively assigned to ans.

MATLAB can't typically take the content from these cells and place them into a single array. We could extract the contents of row 1, one cell at a time as we did to create m. If we want to extract more cells at once, we have to place the contents of each cell into its own separate array, like this,

[c11 c12 c13] = C{1,:}
c11 =
     8     1     6
     3     5     7
     4     9     2
c12 =
    17
c13 =
fred

taking advantage of syntax new in MATLAB Release 14 for assignment when using comma-separated lists.

Cell Array Indexing Summary

  • Use curly braces {} for setting or getting the contents of cell arrays.
  • Use parentheses () for indexing into a cell array to collect a subset of cells together in another cell array.

Here's my mnemonic for remembering when to use the curly braces: curly for contents Does anyone have any mnemonics or other special ways to help remember when to use the different kinds of indexing? If so, please post here.

References


Published with MATLAB® 7.2

65 Responses to “Cell Arrays and Their Contents”

  1. Joey replied on :

    *note: random comment not necessarily related to your recent post*

    Loren:
    I’m a fairly new Matlab user, but I still enjoy reading your postings. Just out of curiousity:

    Is this you? why(61)?

    Engineering freshmen never get tired of this command.

    Joey

  2. Loren replied on :

    Joey-

    I guess so, but I didn’t program that myself!

    –loren

  3. Michael replied on :

    I don’t have another mnemonic for the braces problem, but it helps me when i think about it this way:
    - cells are like wrappers around some content.
    - as Loren said, normal subscripting/indexing using () results in the same data type, which is cell. think of it as of splitting the cell.
    - the curly braces dereference (or extract) the wrapped content. For people familiar with e.g. C++ this might help, as it resembles in some ways dereferencing pointers.

    Just as an aside: sometimes i think notation in matlab is a bit cumbersome. Take e.g. an array of structs. Now you want to assign to a number of these structs the same value to a certain field. currently this can be achieved by:

    [s(1:3).val] = deal(5)

    if i wanted to assign 5 to all fields named ‘val’ in the structs s(1:3). This notation is a bit lengthy and complicated, why not just:

    s(1:3).val = 5

    This would be consistent with the syntax used for numeric arrays and is afaik unambigous.

    Michael

  4. Loren replied on :

    Michael-

    I have entered your suggestion into our enhancement database. I think scalar expansion is generally very useful.

    –Loren

  5. Smith replied on :

    hi,
    your blogs are very nice

  6. Jeff R. replied on :

    Thanks for the great demystification of “cell” and “content” indexing of cell arrays. Your article should become part of the handbook!

  7. erik replied on :

    hey loren-

    i’m wondering why the following syntax does not work:

    x=dir
    a{2:4}={x(3:5).name}

    -erik

  8. Loren replied on :

    Erik-

    Because

    1) MATLAB requires you to use [] for multiple left-hand sides.

    2) You only have a single right-hand side.

    Try this instead:

    [a{2:4}]=x(3:5).name
    

    –Loren

  9. erik replied on :

    ah great. thanks. i understand why my curly’s on the right side were a bad idea. but i don’t understand why the squares are necessary on the left. isn’t the intent unambiguous without them?
    -erik

  10. Loren replied on :

    Perhaps the intent is, but that’s not MATLAB. We could extend it that way, but that might end up being more confusing rather than less. The left-hand side I wrote is actually 3 distinct outputs. MATLAB currently requires multiple outputs to be enclosed between [].

    –l

  11. Oliver A. Chapman, PE replied on :

    This is a wonderful insight! Is there more discussion of this in the documentation? The only thing I could find was this brief line under the brief discussion of square brackets under Special Characters

    “For the use of [ and ] on the left of an “=” in multiple assignment statements, see lu, eig, svd, and so on.”

  12. erik replied on :

    ah ha — i am remembering now why i feel like my original approach should work. why isn’t the following analogy appropriate? single lhs, single rhs.

    b(3:5) = [1 2 3]

    there shouldn’t be any need to invoke the “multiple outputs/comma-separated lists” stuff, should there?
    -erik

  13. Loren replied on :

    As with many things in MATLAB, singletons are treated differently. With one output, you can, but do not need to use, the square brackets.

    See this part of the documentation,
    particularly the section on “MATLAB Function Syntax.” I looked under MATLAB Programming, found a section called “Calling Functions”, went to the section on “MATLAB Calling Syntax” and found it in a subheading in that section.

    –loren

  14. erik replied on :

    right, but i’m trying to say that in:
    (1) a{2:4}={x(3:5).name}

    both sides are singletons, just as in:
    (2) b(3:5) = [1 2 3]

    i don’t get why (1) is unacceptable, since (2) is so standard.
    -erik

  15. Loren replied on :

    Erik-

    Your left-hand side in the first equation is NOT a singleton. It has 3 elements. It’s the same as this comma-separated list:

    a{2},a{3},a{4}
    

    3 arrays with potentially different sizes/types.

    The second case IS a singleton. It’s a 1×3 array. That’s different than 3 arrays as in the first case.

    The cases you state are quite different from one another. It is possible for MathWorks to consider adding the syntax you would like to MATLAB. I don’t yet find it compelling myself.

    –Loren

  16. erik replied on :

    ah, i see, yes my lhs is not singleton. but a(2:4) is. so the following works!

    a(2:4)={x(3:5).name}

    this approach also works pre-R14, unlike the multiple LHS square bracket approach. does this contradict the guideline: “use curlys for setting/getting,” or am i misunderstanding the guideline?

    this is why i have such a hard time using cell arrays — i think “i have a cell array, i have to use curlys.” but then there are these situations where it is much more natural (and legal) to use parens. so the whole cell array distinction breaks down for me. looking at the cell array chapter of the help, i see that this is the “content vs. cell indexing” issue, which i must admit baffles me. as an experienced programmer in many other languages, this whole cell thing is the most irksome part of matlab — other languages do not need a separate syntax/object model for hierarchically nested dynamically/heterogeneously sized compound types. so this complexity in matlab’s design always feels unnatural to me.

    -erik

  17. Loren replied on :

    Erik-

    Look carefully at what you get here. You get the one cell array {x(3:5).name} of size 1×3 scalar expanded so that a(2) equals a(3) and a(4). The contents are duplicated, not distributed among the output vector.

    –Loren

  18. erik replied on :

    unless i’m misunderstanding you, that’s not what i think i’m getting:

    >> x=dir

    x =

    30×1 struct array with fields:
    name
    date
    bytes
    isdir

    >> a(3:5)={x(2:4).name}

    a =

    [] [] ‘AUTOEXEC.BAT’ ‘CONFIG.SYS’ ‘Config.Msi’

    >> a(2)

    ans =

    {[]}

    >> a(3)

    ans =

    ‘AUTOEXEC.BAT’

    >> a(4)

    ans =

    ‘CONFIG.SYS’

    >> a{2}

    ans =

    []

    >> a{3}

    ans =

    AUTOEXEC.BAT

    >> a{4}

    ans =

    CONFIG.SYS

    -erik

  19. Loren replied on :

    Erik-

    Of course, you are right. When I replied I was not thinking clearly at all and thought you had 3 outputs on the left and one right-hand side. You are actually copying part of a cell array to another cell array of the same size - one right-hand side, one left-hand side.

    –Loren

  20. erik replied on :

    hey loren -
    no problem! what you just wrote is a perfect explanation for why a{2:4}=x(3:5).name should work. rather than returning “multiple right hand sides,” i think it would be much clearer if expressions like this always implicitly returned a singleton cell array of results. the whole “multiple this’s and that’s” thing is pretty strange, and i think you’d agree it causes much confusion. are there examples of ambiguous cases where just packaging things up into cell array singletons would not be appropriately expressive?

    i think the confusion is just evidence that the cell array design is not so user-friendly. i know matlab is locked into cell arrays at this point, but hypothetically, would a native string type solve all the issues that cell arrays were designed to solve? just like i can have a multidimensional array of int8’s or structs, i could have a multidimensional array of strings?

    come to think of it, i am attracted to the simplicity of syntax in languages that stick to purity — in lisp, everything is a list. in java/python/smalltalk, everything is an object (well… mostly). in c, everything is a void * (even functions). that seems to me to avoid the difficulties of having stuff jammed into a datatype and you are left unable to figure out how to get it back out or assign it. in matlab, everything could be a multidimensional array that is agnostic as to the types/sizes of its entries — which would themselves be multidimensional arrays. at the bottom would be singleton atomic types — strings, int8’s, structs, doubles, etc. actually, i guess this is what a cell array is — so rather than arguing for getting rid of cell arrays, i’m arguing for getting rid of regular arrays, changing all the builtins to expect cell array arguments instead of numeric matrices, etc. in case i send a multidimensional array that is half strings and half structs to svd(), it could just tell me i’m being silly.

    anyway, since i don’t seriously expect matlab to make such a drastic change, could you summarize how and when to chose parens vs. curlies? the “get and set with curlies” doesn’t seem to be quite right, as per the above example. is there a performance difference or other consideration that gives a preference between
    [a{2:4}]=x(3:5).name
    and
    a(2:4)={x(3:5).name}
    ?

    i am not so fond of the perl philosophy “provide 18 ways to do the same thing.” because when you are trying to understand how to use something and remember it 3 weeks from now, it is just confusing if there are lots of options with very subtle differences (parens vs. curlies, for example) that have no semantic impact — not to mention lots of similar variations that have unambiguous semantic intent, but are syntactically disallowed. this is what makes it hard to “think in matlab,” in my opinion — i can’t just express my natural intent of “stick these three strings in these positions of that array” — i have to worry about curlies and squares and parens and multiple left hand sides, etc. i like freedom and options, but only when they are meaningful and actually let me do new things. for instance, i wouldn’t like it if matlab let me use pound signs and backquotes to stand for equals signs (but only on thursdays), just so i had more “options.” that’s a bit what the paren vs. curly distinction feels like to me right now. two semi-incompatible ways to do indexing feels like one too many. my (very intelligent and skilled programmer) boss refuses to use cell arrays in matlab and will not read or work with my code because i do use them! :)

    that reminds me of one last language design question i have. in the above discussion, rather than having to store x=dir, wouldn’t it be nice to be able to index the result of dir directly? it is a long-standing standard in programming languages that parens are for function args and squares are for indexing. so it would be natural for dir[5].name to mean “the name of the fifth result of dir.” but since matlab uses parens for both function calls and indexing, i will never be able to write dir(5) for these semantics, because that looks like a call to the dir function with an argument of 5. the best i could do is the very ugly dir()(5). and if dir() returns a *function*, i am again stuck. why did matlab choose to overlap the syntax for function args and indexing?

    sorry for the overly philosophical and self-important post. these are just things that have been brewing in the mind of a relatively experienced programmer who has been trying to become proficient at matlab over the past 2 years and sometimes is frustrated by the design decisions — if i understood their rationale then my frustration would go away.
    -erik

  21. erik replied on :

    sorry to be annoying, just thought the following was amusing/illustrative. i remember about a year ago it being late at night and i was under a deadline, and trying to figure out how to use a cell array (for like the third time — it just never sticks). after each line i remember wanting to throw my computer out of the window and thinking “it is so fricking obvious what i want, you obstinate junkpile…”

    the transcript was something like:

    >> a{2:4}=x(3:5).name
    ??? Illegal right hand side in assignment. Too many elements.

    >> a{2:4}={x(3:5).name}
    ??? Insufficient outputs from right hand side to satisfy comma separated
    list expansion on left hand side. Missing [] are the most likely cause.

    >> [a{2:4}]={x(3:5).name}
    ??? Insufficient outputs from right hand side to satisfy comma separated
    list expansion on left hand side. Missing [] are the most likely cause.

    >> a{2:4}=[{x(3:5).name}]
    ??? Insufficient outputs from right hand side to satisfy comma separated
    list expansion on left hand side. Missing [] are the most likely cause.

    >> a{2:4}=deal({x(3:5).name})
    ??? Insufficient outputs from right hand side to satisfy comma separated
    list expansion on left hand side. Missing [] are the most likely cause.

    >> a{2:4}=deal(x(3:5).name)
    ??? Error using ==> deal
    The number of outputs should match the number of inputs.

    >> a{2:4}=[x(3:5).name]
    ??? Insufficient outputs from right hand side to satisfy comma separated
    list expansion on left hand side. Missing [] are the most likely cause.

    >> {a{2:4}}=x(3:5).name
    ??? {a{2:4}}=x(3:5).name
    |
    Error: Missing operator, comma, or semicolon.

    >> {a{2:4}}={x(3:5).name}
    ??? {a{2:4}}={x(3:5).name}
    |
    Error: Missing operator, comma, or semicolon.

    … and so on for many lines…

    all of the above still seem totally reasonable to me. by the end i was reduced to a shaking red eyed ogre and wanted to peel my fingernails off. i just do not have these kinds of problems in c, lisp, java, or even perl. ok maybe a little bit in lisp. the backquote is still hard on me. prolog’s negation and -> semantics are the only fundamental language constructs i can think of right now that torment me like cell arrays.

    incidentally, here is correct way #3:
    [a{2:4}]=deal(x(3:5).name)

  22. Loren replied on :

    Erik-

    The reason for the multiple left-hand sides has to do with keeping consistency across all of MATLAB for the comma-separated list. If you keep that in mind (and don’t answer posts while distracted!), you should find it fairly easy to keep track of.

    The indexing into an expression is something that we have heard before. It is on the enhancement list - no date promised.

    –Loren

  23. Loren replied on :

    Erik-

    Correction for you. An even better way for you to do the data distribution is:

    [a{2:4}]=x(3:5).name
    

    Multiple rhs, multiple lhs.

    –Loren

  24. Mohammad Reza replied on :

    Given I have a cell array “d” which contains a series of column vectors (different lengths) containing dates; I would like to take the union of all these dates.

    If union() took muliple arguments like isequal() then I could write union(d{:}). But since union() only unions two arguments, I’ll have to write somethign like this:

    union(union(union(union(d{1}, d{2}), d{3}), d{4}), …

    Is there a shorthand way to write the above?

    thanks.

    /m

  25. Loren replied on :

    Mohammed-

    I think you could do this instead:

    unique(cat(1,d{:})
    
  26. Mohammad Reza replied on :

    Thats a good way, but there are two issues with that. First unique() tends to do an unwanted sort, and second its not generic enough to directly apply to some other set operation such as intersect() or setdiff().

    I am using the following to do the job:

    s = d{1}; for i = 2:length(d) s = union(s,d{i}); end

    it works, but its not neat!

    /m

    ps. I find your blog inspiring. Thanks.

  27. Arun replied on :

    wow this is interesting ..
    but i had doubt that how to get tha output in the array format.??

  28. Sina replied on :

    Hello!
    I faced a big problem while I worked on a project!
    I have a Cell array in MATLAB which contains Symbolic objects. I wanna print it, and wanna see content of cells! but when I use “celldisp” or when I type the name of cell array, it just show me that it`s components are “sym 1*1″, Or show the contents in different lines ( I mean it doesnt show cell contents in an organized format), How can I see or print this cell array?!
    Tanx!

  29. Loren replied on :

    Sina-

    You currently would need to create your own print routine.

    –Loren

  30. Michael replied on :

    Hi,

    I am a novice MATLAB user, currently using it for an oceanographic project. I have ASCII data from 30 stations, essentially ‘mx7′ matrices, where 98

  31. Michael replied on :

    *attempt to resend; don’t know if initially received*

    Hi,

    I am a novice MATLAB user, currently using it for an oceanographic project. I have ASCII data from 30 stations, essentially ‘mx7′ matrices, where 98

  32. Beat replied on :

    Sina-
    my little script below may help to print your objects.

    %% read fieldnames in cell
    s_fieldnames=fieldnames(S); % S is your Cell array
    s1_fieldnames=char(s_fieldnames);

    %% convert field names in cell array into corresponding variable name
    for m=1:length(s_fieldnames),
    s2_fieldnames=textscan(horzcat(s1_fieldnames(m,:)),’%s’);
    s2_fieldnames=char(s2_fieldnames{1,1});

    %% store original value in variable
    eval([s2_fieldnames,’=getfield(S,”’,char(s_fieldnames(m)),”’);’]);
    end;
    clear S s_fieldnames s1_fieldnames s2_fieldnames m

    % then print the variables from the Matlab workspace

  33. Andre replied on :

    Dear Loren,

    I’m writing a Matlab code which reads an Excel file and stores data in two matrices: ‘a’ and ‘b’, the latter being a cell matrix.

    In the first column of ‘b’ matrix, the user is mandatory to type a 3-character word. In the second column, it is optional to type anything.

    If the user fills the second column of ‘b’, this word must be printed. On the other hand, if one does not fill this second column, the content of the first column must be printed. This is accomplished by using an ‘if’ statement and this is my problem when trying to do so.

    My question is: if the content of a ‘b’ element is empty (the user did not type anything) how can I make an ‘if’ test in order to identify this empty content?
    I tried if b{2,2} = [] but Matlab returned an error message.

    Thank you for your attention.

    Andre

  34. Loren replied on :

    Andre-

    Have you tried if isempty(b{2,2})? If it’s not empty, what’s in there - blanks?

    –Loren

  35. Andre replied on :

    Dear Loren,
    Actually, I didn’t try ‘isempty’.
    I’m going to do this.
    Thank you very much!
    Andre

  36. giuseppe replied on :

    I have a problem with cell array.
    With array, it’s quite easy to reorder.
    For example if I want reorder all the rows by means of a permutation vector perm:
    a(perm,:);

    But if a is a cell array I cannot. I have to copy every single element:
    function reorder(XX,perm)

    n = size(XX,2);
    OO = cell(length(perm),n);

    for ii = 1:length(perm)
    for jj=1:n
    OO{ii,jj} = XX{ii,jj};
    end
    end
    Somebody knows a better technique?
    G

  37. Loren replied on :

    G-

    Check out cellfun.

    –Loren

  38. Katie replied on :

    I have created a large cell that holds sign patterns for 4×4 matrices. The matrices stored in the cell are generated by a loop. Is there a way to tell the loop to store only new matrices that are unique and to discard repeated ones?
    this is what i have so far:
    A=cell (10,10) %for now the size is arbitrary
    for i=1:10 j=1:10
    a=-2;b=2 %create range from -M to M
    v=a+(b-a)*rand(4,1) %create 2 random vectors
    w=a+(b-a)*rand(1,4)
    P=eye(4,4)+v*w %create random matrix P
    J=gallery(’jordbloc’,4,0) %create nilpotent matrix
    A1=P*J*inv(P)
    ans=(A1>0)
    A(i,j)={ans}
    end

    thank yoU!!!!

  39. Loren replied on :

    Katie-

    You probably need to consider using unique or ismember when deciding to put an array into a cell. You might get some ideas from looking at the code in
    this post.

    –Loren

  40. Roberto replied on :

    Hello Loren, very good posts you have.

    I have this “indexing in cell array problem” that I have been trying to solve for some hours already:

    A cell array with lets say 1 row and 3 columns.

    - column 1 has a 10×1 cell array of text (labels).

    - column 2 has a 10×1 matrix of numbers.

    - column 3 is empty and I want to put in it the minimum of column 2 accompanied by its corresponding (same row) text found in column 1.

    I have been doing this (for clarity I omit the the assignation so “ans” would take the result):

    [myCell{1,1}(find(min(myCell{1,2}))) min(myCell{1,2})]

    The problem is it always brings up the minimum and the WRONG label. The label it puts is the first one in column 1 no matter where the minimum is.

    Thank you very much.

  41. Loren replied on :

    Roberto-

    Not sure how you are doing this or if this is even the output you want (because all the labels will be the same since column 2 has only one minimum), but here’s a try.

    myCell =
        'L1'       [ 1]     []
        'L2'       [ 2]     []
        'L2 L1'    [ 3]     []
        'L4'       [ 4]     []
        'L5'       [ 5]     []
        'L6'       [ 6]     []
        'L7'       [ 7]     []
        'L8'       [ 8]     []
        'L9'       [ 9]     []
        'L10'      [10]     []
    
    >> [min2, idx] = min([myCell{:,2}])
    min2 =
         1
    idx =
         1
    >>  myCell{idx, 3} = [num2str(myCell{idx,2}), ' ', myCell{idx, 1} ]
    myCell =
        'L1'     [ 1]    '1 L1'
        'L2'     [ 2]        []
        'L3'     [ 3]        []
        'L4'     [ 4]        []
        'L5'     [ 5]        []
        'L6'     [ 6]        []
        'L7'     [ 7]        []
        'L8'     [ 8]        []
        'L9'     [ 9]        []
        'L10'    [10]        []
    

    –Loren

  42. Roberto replied on :

    Hello Loren, Thanks for your reply.

    Sorry I did not make myself clear. Lets say I am working with this cell:

    myCell =
    ‘Labels’ ‘MEANS’ ‘MIN’
    {3×1 cell} [3×1 double] []
    {3×1 cell} [3×1 double] []
    {3×1 cell} [3×1 double] []
    {3×1 cell} [3×1 double] []

    A 5×3 cell array, correct?

    So I want to extract the minimum from the ‘MEANS’ column for every row of the cell. Also I want that minimum to be placed in the ‘MIN’ column accompanied by the corresponding label from the ‘LABELS’ column. So for example if in the ‘MEANS’ matrix from row 1, element 2 is the minimum, concatenate it with element 2 of the corresponding ‘LABELS’ matrix and place all that in the corresponding ‘MIN’ matrix.
    Now do that for every row.

    The output (for one row) should be something like:

    myCell =
    ‘Labels’ ‘MEANS’ ‘MIN’
    {3×1 cell} [3×1 double] {’OLS ‘ [1.4000]}
    {3×1 cell} [3×1 double] []
    {3×1 cell} [3×1 double] []
    {3×1 cell} [3×1 double] []

    I worked it out with your code like this:

    [minimum indx] = min(myCell{2,2})
    myCell{2,3} = [myCell{2,1}(indx,1) minimum] ;

    but I still don’t understand why this does not work:

    myCell{2,3} = [myCell{2,1}(find(min(myCell{2,2}))) min(myCell{2,2})]

    THANKS.

  43. Loren replied on :

    Roberto-

    The problem is with find(min())… piece. You need to break down the pieces here. min returns a scalar for the 3×1 vector and find then finds the first element in that, with is the first 1. That’s why you always get the first label.

    –Loren

  44. Wolfgang replied on :

    Hi Loren,

    I don’t know whether following has been discussed previously, but, is there a way to preallocate a cell array when the number of matrices in the cell array remain constant but the number of elements in the matrices change? Usually I try following:

    A = cell(20,20);
    A = cellfun(@(x) zeros(100,1),A,’UniformOutput’,false);

    Thereby I allocate a vector with 100 elements for each cell element. Now, the calculations I do may lead to a strong accumulation of values in some matrices while others remain small. Now, how does Matlab handle this? Does it make a difference in the memory allocation, when the matrix in the cell grows to 1000 elements while 10 might be reduced to 0 elements? So far, I do not have the impression that Matlab becomes slow.

    Best regards, Wolfgang

  45. Loren replied on :

    Wolfgang-

    The important thing is to preallocate the cell array itself. Each element in a cell array is effectively a single MATLAB array itself and as look as your cellfun calculation produces each full array for each cell, you should not even need to preallocate the space for each cell array element. Each one will get created via cellfun. You’re now just throwing out your predefined contents and filling them with the new results anyhow.

    –Loren

  46. Joao replied on :

    I Loren,

    I must say I’ve been learning a lot Matlab since I subscribed to your blog. For that, thanks very much.
    I’ve a problem related with cells. I’m trying to read a file witch as several blocks of information and each one of them as its distinctive headerline with the following format

    364,1 “04261056.C.2″

    I’m using textscan to read the file and the output comes as a cell. Is it possible to read from the cell the value 364 into an array?

    Thanks

  47. Vadim replied on :

    Joao,
    If I understand your question correctly, you have either one or more lines of data like this:
    364,1 “04261056.C.2″
    364,1 “04261056.C.2″
    and you may be reading them like this:
    C = textscan(fid,’%d%d%q’, ‘delimiter’, ‘,’)
    The results would come out in a cell array like this:
    [2×1 int32] [2×1 int32] {2×1 cell}

    C{1} is in fact an array of numbers and can be treated as any other array of numbers,
    e.g.
    >> Array = zeros(1,10)
    Array =
    0 0 0 0 0 0 0 0 0 0
    >> Array(1:2) = C{1}
    Array =
    364 364 0 0 0 0 0 0 0 0

    Hope this helps.
    -Vadim

  48. wei replied on :

    Loren, How do convert from nested {{’a',’b'},{’c'},{’d'}} to flat {’a',’b',’c',’d'}?

  49. Ebbe replied on :

    Hi!
    Interesting blog.
    I want to make a function that prints a text file from a cell with different content. I want it to be flexible, so that I can input any type of cell array.
    Is there a way to find out the content in a cell before performing any operations on it. I will give you an example. My cell array contains [Num 1600×1] [String 1600×16] [Num 1600×1] [String 1600×1].
    I want to go through the cell, column by column and if it is numeric, perform ‘cell2mat’ and if it is a string perform ‘char’. I don’t know in advance what is in each column.
    Thanks!
    Ebbe

  50. Loren replied on :

    Ebbe-

    If you *know* that each column is homogeneous, you could do something like this.

    A = { your cell array ...};
    colContents = cellfun(@(x) class(x(1)), A)
    

    to find the content types for each column. Then you can do what you want from that information.

    –Loren

  51. Ebbe replied on :

    Thanks a lot!
    I had to set ‘UniformOutput’ to false:
    colContents = cellfun(@(x) class(x(1)), A,’UniformOutput’,false);
    Then it worked!
    Thanks again!
    Ebbe

  52. marc replied on :

    Hi

    I have a small problem. I just started using cell arrays and I’m just lost.

    I have an array P1 for which I want to do the following

    P1 = [ 2 3 4 0
    5 8 0 0
    2 3 1 1];

    K = P1(:,4)==0 & P1(:,3)~=0;
    Z = 1:size(P1,1);
    I = Z(K);
    P1((P1(:,3)==0 | P1(:,4)~=0),:)=[];

    so i’ll get

    I = 1

    P1 = [ 2 3 4 0 ]

    and now I have 20 arrays like P1 but different sizes, so I can build a 3 dimensional cell array

    P = {P1 P2 P3 …}

    is there any way to use indices to perform the same calculations on every P?

    Thanks

    Marc

  53. Loren replied on :

    Marc-

    I don’t see where you have all the arrays to start. But if you put the initial arrays into a cell array, you can use cellfun to do your calculations and place the outputs into a cell array.

    –Loren

  54. marc replied on :

    Loren

    Thank you so much for the response.

    My real stat cell array is

    cell 1,1 [69881×13 duble]
    cell 1,2 [70160×13 duble]
    cell 1,3 [69555×13 duble]

    I’ll try to use the cellfun.

    Marc

  55. Arco replied on :

    Hi Loren,
    I am impressed with your answers to the problems on this topic and hope you can help me with a problem I have been struggling with for some time now.

    I have a function that evaluates a given function handle (fHandle) and provides some extra output. At some point I evaluate the function handle

    temp = cell(1,nargout-1);
    temp(1:nargout-1) = {feval(fHandle,returns,nDates)};
    

    The problem is that the function handle should return a cell array with arrays of more than two dimensions, but it only returns arrays of two dimensions. If I run the following

    [a b c] = {feval(fHandle,returns,nDates)};
    

    a and c are 100 X 100 and b is 100 X 100 X 5 as they should be; in my first example temp{1}, temp{2} and temp{3} are all 100 X 100, while temp{2} should also be 100 X 100 X 5 as is b.

    It is not possible for me to use [a b c] as different function handles have different number of output arguments.

    Thanks in advance

  56. Loren replied on :

    Arco-

    I don’t understand your code, esp. the right-hand side being enclosed in {}. If you can post the SMALLEST amount of code to reproduce the situation, that would be helpful. If it’s really long, I recommend you contact support (link on the side of the page).

    –Loren

  57. Scott replied on :

    Loren,

    I have a large cell array that I initialized using the cell(m,n) command. Will Matlab need to reallocate memory for the entire array each time I change or add an element?

    Thanks,
    Scott

  58. Loren replied on :

    Scott-

    In this case, there are 2 ways in which MATLAB may have to allocate/reallocate memory. 1) if you change teh size of the cell array itself, a reallocation will take place, and 2) MATLAB will allocate memory for each element you assign to. Currently all the cells are empty.

    –Loren

  59. Oleg replied on :
     wei replied on April 30th, 2009 at 16:30 UTC :
     Loren, How do convert from nested {{’a',’b'},{’c'},{’d'}} to flat {’a',’b',’c',’d'}?
    

    I have the same problem to solve, couldn’t find any solution with cellfun, just with nested loop (for-end) for every cell nesting.

  60. Loren replied on :

    Oleg and Wei-

    There is no good way that I know of. You’d need to write a function that would keep going through levels of a cell array until no more cells are found.

    –Loren

  61. Oleg replied on :
     Oleg and Wei-
    
    There is no good way that I know of. You’d need to write a function that would keep going through levels of a cell array until no more cells are found.
    
    –Loren
    

    I feared that would be the answer. That means a good opportunity to write a “nest2flat” function. If interested, as soon as i write it, i’ll make it available on filesharing.

  62. Oleg replied on :

    To flatten

    {a}, b, {c}}} --> {a,b,c}
    

    the simplest command is

    [cell{:}]
    

    but it fails to flatten more complex cell arrays/matrices like

    {{a}, b, {c;d}}}.
    

    I wrote a function which deals with any type of nested cells. If interested look for nested cell in the file exchange.

    Oleg

  63. Yair Altman replied on :

    Oleg - this is actually a fairly simple exercise in cellfun recursion:

    function data = decellify(data)
      try
          data = cellfun(@decellify,data,'un',0);
          if any(cellfun(@iscell,data))
              data = [data{:}];
          end
      catch
          % a non-cell node, so simply return node data as-is
      end
    

    This works very quickly, for any type of input (cell/non-cell), and any type of data (numeric/strings/…).

    Yair Altman

  64. Oleg replied on :

    To Yair:
    i discovered the recursive method right after i submitted my function. Bruno Luong exposed the same method as a comment to my function.
    Thanks again.

  65. Shlomi replied on :

    Hello Loren and thanks for your blog.
    It gives me much insight and inspiration.

    Please some more attention to the “de-celling’, (AKA “decellifying” a-la Altman).

    It really bothers me when trying to enjoy the so-powerful feature of cell arrays.
    Is there a simple way to achieve this task:

    Values = [1 2 3]   % or whatever, a numeric single-row array
    % I want to get as result a cell c such that:
    %   c = { 'Values' 1  2  3  }
    %   or, more precisely:  c = { 'Values' [1]  [2]  [3]  }
    %    i.e.  1x4 cell array.
    % the shortest way I found is
    tmp = num2cell(Values)
    c = { 'Values'  tmp{:}  }
    

    Such cell arrays, with row label or title string, are most valuable when outputing the results by the MATLAB rptgen, or even with MS-EXCEl, which can handle none-nested cells.

    TIA
    Shlomi

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Jun: I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
  • Loren: Wow folks- Always lots of interest when there’s a quickie to try out! I will only make 2 general...
  • Loren: Jun- ismember is your friend here: >> [aa,ind] = ismember(Array2,Arra y1) aa = 1 1 1 1 1 1 1 ind = 1 2 1 4 4 3...
  • Dan: I like the first way better than the second way. Combining the arrays into one and running any is nice, although...
  • James Myatt: How about I = (a == 0 | b == 0); a(I) = []; b(I) = [];
  • Tunc: Hello Loren, love your blog because of such inspiring and challenging comments to such ’small’...
  • Pekka Kumpulainen: Here is my tradeoff. I usually want to keep the original variables as they are most probably...
  • Iain: Followup: Of course, to allow NaNs (counting them as non-zero): mask = (a~=0) & (b~=0); The mask says “a...
  • Matt Fig: I would usually go with something like this: y = a&b; x = a(y); y = b(y); But I was surprised to find...
  • kk: c=all([a;b]) a(c) a(b)

These postings are the author's and don't necessarily represent the opinions of The MathWorks.