MATLAB Function Reference |

Execute a string containing a MATLAB expression in a workspace

**Syntax**

**Description**

`evalin(ws,`

executes

)
*expression*

, a string containing any valid MATLAB expression, in the context of the workspace *expression*`ws`

. `ws`

can have a value of '`base`

' or '`caller`

' to denote the MATLAB base workspace or the workspace of the caller function. You can construct

by concatenating substrings and variables inside square brackets:*expression*

`[a1,a2,a3,...] = evalin(ws,`

executes

)
*expression*

and returns the results in the specified output variables. Using the *expression*`evalin`

output argument list is recommended over including the output arguments in the expression string:

The above syntax avoids strict checking by the MATLAB parser and can produce untrapped errors and other unexpected behavior.

`evalin(ws,`

executes *expression**,*

)
*catch_expr*

and, if an error is detected, executes the *expression*

string. If *catch_expr*

produces an error, the error string can be obtained with the *expression*`lasterr`

function. This syntax is useful when

is a string that must be constructed from substrings. If this is not the case, use the *expression*`try...catch`

control flow statement in your code.

**Remarks**

The MATLAB base workspace is the workspace that is seen from the MATLAB command line (when not in the debugger). The caller workspace is the workspace of the function that called the M-file. Note, the base and caller workspaces are equivalent in the context of an M-file that is invoked from the MATLAB command line.

**Examples**

This example extracts the value of the variable `var`

in the MATLAB base workspace and captures the value in the local variable `v`

:

**Limitation**

`evalin`

cannot be used recursively to evaluate an expression. For example, a sequence of the form `evalin('caller','evalin(''caller'',''x'')')`

doesn't work.

**See Also**

`assignin`

, `catch`

, `eval`

, `feval`

, `lasterr`

, `try`

evalc | eventlisteners (COM) |