deal

Distribute inputs to outputs

Description

example

Note

In most cases, you do not need the deal function. Instead, use array indexing and comma-separated lists when accessing the contents of cell arrays and structure fields, as shown in these syntaxes.

[A1,...,An] = C{:}
[B1,...,Bn] = S(:).field

However, in a few cases deal can be useful when you need multiple outputs from an operation that typically returns a single output. For example, you can use deal to:

• Assign values to multiple cells of a cell array.

• Assign values to a field of a nonscalar structure array.

• Return multiple outputs from an anonymous function.

[B1,...,Bn] = deal(A1,...,An) copies the input arguments A1,...,An and returns them as the output arguments B1,...,Bn. It is the same as B1 = A1, …, Bn = An. In this syntax, the numbers of input and output arguments must be the same.

[B1,...,Bn] = deal(A) copies the single input argument A and returns it as the output arguments B1,...,Bn. It is the same as B1 = A, …, Bn = A. In this syntax, you can specify an arbitrary number of output arguments.

Examples

collapse all

Create a 1-by-3 cell array whose cells are empty.

C = cell(1,3)
C=1×3 cell array
{0x0 double}    {0x0 double}    {0x0 double}

Create three variables. Assign their values to cells of C by using deal. The syntax C{:} creates a comma-separated list of cells whose contents you can access by using curly braces. You can treat this list as a list of output arguments when calling deal.

A1 = 100;
A2 = "foo";
A3 = 1:5;
[C{:}] = deal(A1,A2,A3)
C=1×3 cell array
{[100]}    {["foo"]}    {[1 2 3 4 5]}

You can also use indices to assign values to a subset of cells. For example, create a 1-by-5 cell array D.

D = cell(1,5)
D=1×5 cell array
{0x0 double}    {0x0 double}    {0x0 double}    {0x0 double}    {0x0 double}

This syntax assigns values to the first three cells of D and leaves the remaining cells empty.

[D{1:3}] = deal(A1,A2,A3)
D=1×5 cell array
{[100]}    {["foo"]}    {[1 2 3 4 5]}    {0x0 double}    {0x0 double}

First create a 1-by-3 structure array with one field.

S.a = [];
S(2).a = [];
S(3).a = [];
S.a
ans =

[]

ans =

[]

ans =

[]

Then assign values to the fields by using deal. The syntax S.a creates a comma-separated list of fields, one for each structure in the structure array. You can treat this list as a list of output arguments when calling deal.

A1 = 100;
A2 = "foo";
A3 = 1:5;
[S.a] = deal(A1,A2,A3)
S=1×3 struct array with fields:
a

S.a
ans = 100
ans =
"foo"
ans = 1×5

1     2     3     4     5

If the structure array does not exist, then you can create it by specifying enough indices to accommodate the values you assign. You can also use indices to assign values to the fields of a subset of structures. For example, if S were a 1-by-6 structure array, this syntax would assign values to the first three structures.

[S(1:3).a] = deal(A1,A2,A3)
S=1×3 struct array with fields:
a

S.a
ans = 100
ans =
"foo"
ans = 1×5

1     2     3     4     5

You can create an anonymous function to define a function without creating a program file, as long as the function contains a single executable statement. One way to return multiple output arguments from an anonymous function is to call the deal function as that single executable statement.

Create an anonymous function that returns a number and its square, cube, and fourth power by using the deal function. To store the anonymous function, assign it to a function handle.

powers1234 = @(x) deal(x,x.^2,x.^3,x.^4)
powers1234 = function_handle with value:
@(x)deal(x,x.^2,x.^3,x.^4)

Call powers1234 and assign its outputs to variables. You must assign all outputs that deal returns.

[X1,X2,X3,X4] = powers1234(2)
X1 = 2
X2 = 4
X3 = 8
X4 = 16

To discard unwanted outputs, use the ~ operator.

[~,~,X3,X4] = powers1234(2)
X3 = 8
X4 = 16

Input Arguments

collapse all

Input arrays, where n is the number of inputs.

Output Arguments

collapse all

Output arrays, where n is the number of outputs.

Tips

• To convert a cell array to a structure array, or a structure array to a cell array, use the cell2struct and struct2cell functions, not deal.

Version History

Introduced before R2006a