evalc - Evaluate MATLAB expression and capture results - MATLAB (original) (raw)

Evaluate MATLAB expression and capture results

Syntax

Description

[results](#mw%5Febcbd47e-e0fd-49dc-983d-8d640cd5cf10) = evalc([expression](#mw%5Fe797d6ab-5ed3-49a7-9df1-db85f718ec92)) evaluates the MATLAB® code represented by expression and captures anything that would normally be written to the Command Window inresults.

Note

Security Considerations: When callingevalc 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:

Performance Considerations: In most cases, using theevalc 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 toevalc.

example

[[results](#mw%5Febcbd47e-e0fd-49dc-983d-8d640cd5cf10),[output1,...,outputN](#mw%5F44d10695-080d-499e-87c0-c68df3b42cae)] = evalc([expression](#mw%5Fe797d6ab-5ed3-49a7-9df1-db85f718ec92)) additionally returns the outputs from expression in the specified variables.

Examples

collapse all

Evaluate Expression

Use evalc to evaluate the expressionmagic(5) and store the results.

results = evalc('magic(5)')

results =

'
 ans =
 
     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

collapse all

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 iskeyword function.

Example: evalc('magic(5)')

Output Arguments

collapse all

results — Captured Command Window output

character array

Captured Command Window output, returned as a character array. Individual lines in the captured output are separated by \n characters.

output1,...,outputN — Outputs from evaluated expression

any MATLAB data type

Outputs from evaluated expression, returned as any MATLAB data type.

Limitations

Tips

Extended Capabilities

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced before R2006a