user379362
user379362

Reputation: 341

Define multiple variables at the same time in MATLAB?

I don't know if MATLAB can do this, and I want it purely for aesthetics in my code, but can MATLAB create two variables at the same time?

Example

x = cell(4,8);  
y = cell(4,8);

Is there a way to write the code something similar to:

x&y = cell(4,8);

Upvotes: 17

Views: 56948

Answers (2)

O. Th. B.
O. Th. B.

Reputation: 1353

It depends on the function that you use to generate the data. You can create your own function in MATLAB that has more than one output:

[a, b, c] = foo();

Many builtin function also have this option. But this must be supported directly by the returning function.

Upvotes: 3

Andrew Janke
Andrew Janke

Reputation: 23858

Use comma-separated lists to get multiple variables in the left hand side of an expression.

You can use deal() to put multiple assignments one line.

[x,y] = deal(cell(4,8), cell(4,8));

Call it with a single input and all the outputs get the same value.

[x,y] = deal( cell(4,8) );

>> [a,b,c] = deal( 42 )
a =
    42
b =
    42
c =
    42

Upvotes: 34

Related Questions