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 forwardName 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

*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
Joey-
I guess so, but I didn’t program that myself!
–loren
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
Michael-
I have entered your suggestion into our enhancement database. I think scalar expansion is generally very useful.
–Loren
hi,
your blogs are very nice
Thanks for the great demystification of “cell” and “content” indexing of cell arrays. Your article should become part of the handbook!
hey loren-
i’m wondering why the following syntax does not work:
x=dir
a{2:4}={x(3:5).name}
-erik
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
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
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
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.”
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
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
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
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
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
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
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
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
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
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)
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
Erik-
Correction for you. An even better way for you to do the data distribution is:
[a{2:4}]=x(3:5).nameMultiple rhs, multiple lhs.
–Loren
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
Mohammed-
I think you could do this instead:
unique(cat(1,d{:})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.
wow this is interesting ..
but i had doubt that how to get tha output in the array format.??
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!
Sina-
You currently would need to create your own print routine.
–Loren
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
*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
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
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
Andre-
Have you tried if isempty(b{2,2})? If it’s not empty, what’s in there - blanks?
–Loren
Dear Loren,
Actually, I didn’t try ‘isempty’.
I’m going to do this.
Thank you very much!
Andre
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
G-
Check out cellfun.
–Loren
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!!!!
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
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.
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
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.
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
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
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