Main Content

evaluateAndCapture

Evaluate MATLAB code in specified workspace

Since R2025a

    Description

    outputDisplay = evaluateAndCapture(w,expression) evaluates the specified code in the workspace w and returns a string containing the output as it would appear in the Command Window.

    Note

    Security Considerations: Before calling evaluateAndCapture 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 evaluateAndCapture. First, search for allowed operations. Then, if you find other operations, disallow execution.

    • Replace evaluateAndCapture with an alternative. For more information, see Alternatives to the eval Function.

    Performance Considerations: In most cases, using the evaluateAndCapture 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 to evaluateAndCapture.

    example

    [outputDisplay,output1,...,outputN] = evaluateAndCapture(w,expression) returns a string containing the output as it would appear in the Command Window and assigns the outputs from the evaluated expression to the specified variables.

    example

    Examples

    collapse all

    Create a set of variables in the base workspace.

    a = 1;
    b = 2;

    Create a workspace object for the base workspace. Then evaluate the expression a+b using the workspace w.

    w = matlab.lang.Workspace.baseWorkspace;
    outputDisplay = evaluateAndCapture(w,"a+b")
    outputDisplay = 
        "
         ans =
         
              3
         
         "
    
    

    Create a set of variables in the base workspace.

    a = 1;
    b = 2;

    Create a workspace object for the base workspace. Then evaluate the expression a+b in the workspace w. Assign the output from the evaluated expression to a variable.

    w = matlab.lang.Workspace.baseWorkspace;
    [outputDisplay,absum] = evaluateAndCapture(w,"a+b")
    outputDisplay = 
    ""
    
    absum = 
    3
    

    Input Arguments

    collapse all

    Workspace, specified as a matlab.lang.Workspace object.

    Expression to evaluate, specified as a string scalar or character vector.

    Output Arguments

    collapse all

    Displayed output, returned as a string scalar containing the output as it would be displayed in the Command Window.

    Outputs from evaluated expression, returned as values of any data type.

    Limitations

    • The input expression cannot use the functions diary, more, and input.

    • Avoid using evaluateAndCapture recursively to evaluate an expression. Doing so might result in unexpected behavior.

    • If you use evaluateAndCapture within an anonymous function, nested function, or function that contains a nested function, the evaluated expression 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 behavior, do not include output arguments in the input to the evaluateAndCapture function. For example, statements like evaluateAndCapture(w,['output = ',expression]) are not recommended.

      Instead, specify output arguments of the evaluateAndCapture function to store the results of the evaluated expression. For example:

        [outputDisplay,output1] = evaluateAndCapture(w,expression)

    Version History

    Introduced in R2025a