Main Content
This example shows how to add variables to the MATLAB® engine workspace in Python®.
When you start the engine, it provides an interface to a collection of all MATLAB variables. This collection, named workspace
, is implemented as a Python dictionary
that is attached to the engine. The name of each MATLAB variable becomes a key in the workspace
dictionary. The keys in workspace
must be valid MATLAB identifiers [e.g., you cannot use numbers as keys]. You can add variables to the engine workspace in Python, and then you can use the variables in MATLAB functions.
Add a variable to the engine workspace.
import matlab.engine eng = matlab.engine.start_matlab[] x = 4.0 eng.workspace['y'] = x a = eng.eval['sqrt[y]'] print[a]
In this example, x
exists only as a Python variable. Its value is assigned to a new entry in the engine
workspace, called y
, creating a MATLAB variable. You can then call the MATLAB eval
function to execute the sqrt[y]
statement in MATLAB and return the output value, 2.0, to Python.
See Also
matlab.engine.MatlabEngine
|
matlab.engine.FutureResult
Related Topics
- Call MATLAB Functions from Python
- Sort and Plot MATLAB Data from Python
You can call any MATLAB function as a method of a MatlabEngine
object. The engine dynamically invokes a MATLAB function when you call it. The syntax shows positional, keyword, and output arguments of a function call.
ret =
MatlabEngine.
matlabfunc
[*args
,nargout=1,background=False,stdout=sys.stsdout,stderr=sys.stderr]
Replace
with the name of any MATLAB function [such as matlabfunc
isprime
or sqrt
]. Replace
with input arguments for the MATLAB function you call. The keyword arguments specify:*args
The number of output arguments the function returns
Whether the engine calls the function asynchronously
Where the engine sends standard output and standard error coming from the function
Specify keyword arguments only when specifying values that are not the default values shown in the syntax.
Input Arguments to MATLAB Function
| Input arguments to MATLAB function, specified as positional arguments | Any Python types that the engine can convert to MATLAB types |
Keyword Arguments to Engine
| Number of output arguments from MATLAB function |
Default: |
| Flag to call MATLAB function asynchronously
|
Default: |
| Standard output |
Default: |
| Standard error |
Default: |
Output Arguments
Python variable | One output argument from MATLAB function | Default values |
| Multiple output arguments from MATLAB function |
|
| No output argument from MATLAB function |
|
| A placeholder for output arguments from asynchronous call to MATLAB function |
|