Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Deal or No Deal

This post continues in the theme from my last post, where people routinely come to me on a topic. Today I want to distinguish
between indexed assignment, where you can take advantage of scalar expansion, and assignment to several output arrays, often
arising from a comma-separated list from cell or struct arrays.

 

Contents

First Explore Cell Arrays

Let's first explore some aspects of creating and populating the contents of a cell array. I first initialize 2 arrays, each
of length 3.

a = {1 magic(3) 'hello'};
d = {'hi' 17 pi};

Next I initialize another cell array to be the same as a.

c = a;

Now I attempt to change the contents of all the cells in c by assigning a new value that I want to show up in each cell.

try
    [c{:}] = 'aloha'; % error unless c is a scalar
catch cellE
    disp(cellE.message)
end
Too many output arguments.

As you can see, this didn't work. The reason is that I have 3 entities on the left-hand side, because c is a cell array of length 3 and I used the comma-separated list notion. MATLAB doesn't know how to make 3 separate arrays
out of 1 by simple assignment.

I could use regular indexing on c to populate it however. In this case, the right-hand side has length 1 (a cell array with string contents) and the left-hand
side is all of c. MATLAB can do scalar expansion for regular assignment into arrays which is why this works.

c(:) = {'ahola'}
c = 
    'ahola'    'ahola'    'ahola'

How else might we populate all the cells in c? One way is to use the function deal. I prefer the line above; I think the code without deal is clearer and it doesn't have the extra function call overhead.

[c{:}] = deal('ola')
c = 
    'ola'    'ola'    'ola'

If I have the values I want to store in the contents of c in another cell array, let's say d, I can again use deal but I don't have to. Compare the following 2 statements.

[c{:}] = deal(d{:})
[c{:}] = d{:}
c = 
    'hi'    [17]    [3.1416]
c = 
    'hi'    [17]    [3.1416]

In the first of these, I expand the cell array on the right to a comma-separated list and pass it through the function deal which then distributes the outputs into c. In the second statement, I bypass the function call to deal with some relatively new (MATLAB 7) syntax.

Now Explore structs

Let's do a similar exercise with structs now.

s = struct('f',a);
s.f
ans =
     1
ans =
     8     1     6
     3     5     7
     4     9     2
ans =
hello
try
    [s.f] = 'bonjour'; % error unless s is a scalar
catch structE
    disp(structE.message)
end
Too many output arguments.
[s.f] = deal('hej');
s.f
ans =
hej
ans =
hej
ans =
hej
[s.f] = d{:};
s.f
ans =
hi
ans =
    17
ans =
    3.1416

As you can see, I can bypass the function deal when assigning to a struct array in analogous way to cell array assignment.

References

Here are some prime references for getting more details on today's topic.

Why Do You Use deal?

I'd like to hear from you about situations where you feel using deal is your only option or you prefer it (perhaps readability?) to the no deal syntax. Let me know here.

Published with MATLAB® 7.5


  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。