assignin

Assigns a value to a variable in the base or caller workspace.

Category: Programming > Functions

Syntax

assignin(ws, name, value)

Creates or replaces variable name in workspace ws.

Description

A function has its own workspace, so it cannot normally change the variables of the code that called it. assignin is the exception: it writes into the base workspace or into the caller's. This makes data flow invisible in the code, so prefer returning values from the function. It is mainly used in callbacks and debugging helpers.

Input arguments

ws
'base' for the base workspace or 'caller' for the workspace of the calling function.
name
Variable name; it must be a valid name.
value
Any value.

Example

A function writing into the caller

writelines(["function setit()", "  assignin('base', 'fromfn', 42);", "  assignin('caller', 'c1', 7);", "end"], 'setit.m');
setit
fromfn
c1
Output
fromfn = 42
c1 = 7

See also

evalin inputname eval who