evalin

Evaluates code in the base workspace or in the workspace of the caller.

Category: Programming > Programming Utilities

Syntax

evalin(ws, expression)

Runs expression in the workspace named by ws.

evalin(ws, expression, catchExpr)

Runs catchExpr in the same workspace if expression fails.

Description

Inside a function, variables of other workspaces are not visible. evalin reaches into the base workspace or the caller to read or set them. Because it hides data flow between functions, it is best kept for debugging helpers and framework code.

Input arguments

ws
'base' for the base workspace or 'caller' for the workspace of the function that called the current one.
expression
Text with the code to run.

Example

Reading from base and from the caller

function inner()
    evalin('caller', 'disp(secret)');
end
function outer()
    secret = 9;
    inner();
end
x = 10;
evalin('base', 'disp(x)')
outer()
Output
    10
     9

See also

eval assignin exist who