evalin - Evaluate MATLAB expression in specified workspace - MATLAB (original) (raw)
Evaluate MATLAB expression in specified workspace
Syntax
Description
evalin([workspace](#mw%5F29b55d3e-dc8b-4e6a-b17a-4e109c8ef358),[expression](#mw%5F26f65dda-10ae-4d33-a50c-e728bf0b2ab1))
evaluates the MATLABĀ® code represented by expression
using the variables in the specified workspace.
Note
Security Considerations: Before callingevalin
with untrusted user input, validate the input to avoid unexpected code execution. Examples of untrusted user input are data from a user you might not know or from a source you have no control over. If you need to address this concern, consider these approaches:
- Validate inputs to
evalin
. First, search for allowed operations. Then, if you find other operations, disallow execution. - Replace
evalin
with an alternative. For more information, see Alternatives to the eval Function.
Performance Considerations: In most cases, using theevalin
function is less efficient than using other MATLAB functions and language constructs, and the resulting code can be more difficult to read and debug. Consider using an alternative toevalin
.
[[output1,...,outputN](#mw%5Fe8db20c7-7ed5-4a55-934e-153ad97e906c)] = evalin([workspace](#mw%5F29b55d3e-dc8b-4e6a-b17a-4e109c8ef358),[expression](#mw%5F26f65dda-10ae-4d33-a50c-e728bf0b2ab1))
returns the outputs from expression
in the specified variables. In order for this syntax to be valid the expression,`output1,...,outputN` =`expression`
, must also be valid.
Examples
Use the evalin
function to get the value of a variable in the MATLAB base workspace and store it in a new variable.
Define var
as the 5-by-5 matrix returned by themagic
function.
Get the value of the variable var
in the MATLAB base workspace and store it in the variablev
.
v =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Input Arguments
Workspace in which to evaluate expression, specified as'base'
or 'caller'
.
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 currently running function. The base and caller workspaces are equivalent in the context of a function that is invoked from the MATLAB command line.
Note
If you use evalin('caller',expression)
in the MATLAB debugger after having changed your local workspace context with dbup or dbdown, MATLAB evaluates the expression in the context of the function that is one level up in the stack from your current workspace context.
Expression to evaluate, specified as a character vector or string scalar.expression
must be a valid MATLAB expression and must not include any MATLAB keywords. To determine whether a word is a MATLAB keyword, use the iskeyword function.
Example: evalin('base','magic(5)')
Output Arguments
Outputs from evaluated expression, returned as any MATLAB data type.
Limitations
evalin('caller',expression)
finds only_variables_ in the caller's workspace; it does not find_functions_ in the caller. For this reason, you cannot useevalin
to construct a handle to a function that is defined in the caller.evalin
should not be used recursively to evaluate an expression and doing so may result in unexpected behavior.- If you use
evalin
within an anonymous function, nested function, or function that contains a nested function, the evaluatedexpression
does not create any variables.
Tips
- To allow the MATLAB parser to perform stricter checks on your code and avoid untrapped errors and other unexpected behaviors, do not include output arguments in the input to the
evalin
function. For example, the statementevalin('base',['output = ',expression])
is not recommended.
Instead, specify output arguments to theevalin
function to store the results of the evaluated expression. For example:
output = evalin('base',expression)
Version History
Introduced before R2006a