assignin

Assigns the value val to variable name var in context con.

Syntax

assignin(con, var, val)

Inputs

con
Context in which to assign. Can be 'caller' or 'base'.
Type: char | string
Dimension: string
var
Variable to assign.
Type: char | string
Dimension: string
val
Value assigned to variable var.
Type: double | integer | char | string | logical | struct | cell
Dimension: scalar | vector | matrix

Examples

Base scope assignin example:
function func_one(a)
assignin('base', 'z', a);
end

function func_two(a)
z=2;
func_one(a);
z % unmodified since scope was base
end

z=1;
func_two(3);

z % modified since scope was base
z = 2
z = 3
Caller scope assignin example:
function func_one(a)
assignin('caller', 'z', a);
end

function func_two(a)
z=2;
func_one(a);
z % modified since scope was caller
end

z=1;
func_two(3);
z % unmodified since scope was caller
z = 3
z = 1

Comments

If con is 'base', the assignment is performed in the base scope (outside of any function scopes).

If con is 'caller', the assignment is performed in the scope in which the current function is called.