Evaluate MATLAB expression in specified workspace
Security Considerations: When calling
evalin with untrusted user input, validate the input to avoid
unexpected code execution. Examples of untrusted user input are data coming 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.
evalinwith an alternative. For more information, see Alternatives to the eval Function.
Performance Considerations: In most cases, using the
evalin function is also 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 to
Evaluate Variable Name in Base Workspace
evalin function to get the value
of a variable in the MATLAB base workspace and store it in a new variable.
var as the 5-by-5 matrix returned by the
var = magic(5);
Get the value of the variable
var in the MATLAB base workspace and store it in the variable
v = evalin('base','var')
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
workspace — Workspace in which to evaluate expression
Workspace in which to evaluate expression, specified as
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.
expression — Expression to evaluate
character vector | string scalar
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
output1,...,outputN — Outputs from evaluated expression
any MATLAB data type
Outputs from evaluated expression, returned as any MATLAB data type.
evalin('caller',expression)finds only variables in the caller's workspace; it does not find functions in the caller. For this reason, you cannot use
evalinto construct a handle to a function that is defined in the caller.
evalincannot be used recursively to evaluate an expression. For example, statement
If you use
evalinwithin an anonymous function, nested function, or function that contains a nested function, the evaluated
expressiondoes not create any variables.
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
evalinfunction. For example, the statement
evalin('base',['output = ',expression])is not recommended.
Instead, specify output arguments to the
evalinfunction to store the results of the evaluated expression. For example:
output = evalin('base',expression)
Introduced before R2006a