Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Assign value to variable in specified workspace

`assignin(ws,var,val)`

`assignin(`

assigns the value `ws`

,`var`

,`val`

)`val`

to the variable `var`

in the workspace `ws`

. For example, `assignin('base','x',42)`

assigns the value 42 to the variable `x`

in the MATLAB^{®} base workspace.

If `val`

requires evaluation, MATLAB evaluates it in the function that calls `assignin`

, not in the workspace specified by `ws`

. If `val`

is a function handle, it must be evaluable in the function that calls `assignin`

.

The `assignin`

function is useful for these tasks:

Exporting data from a function to the base workspace.

From within a function, changing the value of a variable that is defined in the workspace of the caller function. For example, you can change the value of a variable in the calling-function argument list.

The

`assignin`

function does not assign values to specific elements of an array. Therefore`var`

cannot contain array indices. This code results in an error.X = 1:8; assignin('base','X(3:5)',-1);

To assign values to specific elements of an array, use the

`evalin`

function.evalin('base','X(3:5) = -1')