deal
Copies input values to separate output variables.
Category: Language Fundamentals > Data Types
Syntax
[a, b, ...] = deal(x)
Gives every output a copy of x.
[a, b, ...] = deal(x1, x2, ...)
Assigns each input to the matching output.
Description
deal is most useful with comma-separated lists: [s.x] = deal(0) sets field x in every element of a struct array, and [c{:}] = deal(5) fills every cell of a cell array. It also allows swapping or distributing values in one line: [a, b] = deal(b, a).
If the numbers of inputs and outputs disagree, and there is more than one input, an error is raised.
Input arguments
x, x1, x2, ...- Any values. With several inputs, their number must equal the number of outputs.
Output arguments
a, b, ...- The distributed values.
Example
Copy, distribute and fill
[a, b, c] = deal(1, 2, 3)
[p, q] = deal(7)
c = cell(1, 2);
[c{:}] = deal(5);
c
Output
a = 1
b = 2
c = 3
p = 7
q = 7
c = 1×2 cell
{[5]} {[5]}