Use the MATLAB® Engine API for Python® to call any MATLAB function on the MATLAB path.
If the MATLAB function is not on the path, you can call it from the current folder. For example, to call MATLAB function myFnc
in folder myFolder
, type:
import matlab.engine eng = matlab.engine.start_matlab[] eng.cd[r'myFolder', nargout=0] eng.myFnc[]
Return Output Argument from MATLAB Function
You can call any MATLAB function directly and
return the results to Python. For example, to determine if a number is prime, use the engine to call the isprime
function.
import matlab.engine eng = matlab.engine.start_matlab[] tf = eng.isprime[37] print[tf]
Return Multiple Output Arguments from MATLAB Function
When you call a function with the engine, by default the engine returns a single output argument. If you know that the function can return multiple arguments, use the nargout
argument to specify the number of output
arguments.
To determine the greatest common denominator of two numbers, use the gcd
function. Set nargout
to return the three output arguments from gcd
.
import matlab.engine eng = matlab.engine.start_matlab[] t = eng.gcd[100.0,80.0,nargout=3] print[t]
Return No Output Arguments from MATLAB Function
Some MATLAB functions return no output arguments. If the function returns no arguments, set nargout
to 0.
Open the MATLAB Help browser from Python.
import matlab.engine eng = matlab.engine.start_matlab[] eng.doc[nargout=0]
The
MATLAB doc
function opens the browser, but does not return output arguments. If you do not specify nargout=0
, the engine raises an error.
Stop Execution of Function
To stop execution of a MATLAB function press Ctrl+C. Control returns to Python.
Use Function Names for MATLAB Operators
You can use a
MATLAB operator in Python by calling the equivalent function. For a list of operators and associated function names, see MATLAB Operators and Associated Functions. For example, to add two numbers, use the plus
function instead of the +
operator.
import matlab.engine eng = matlab.engine.start_matlab[] a = 2 b = 3 eng.plus[a,b]
See Also
matlab.engine.MatlabEngine
|
matlab.engine.FutureResult
Related Topics
- Call MATLAB Functions Asynchronously from Python
- Call User Scripts and Functions from Python
- Use MATLAB Arrays in Python
- Sort and Plot MATLAB Data from Python
Main Content
This example shows how to call a MATLAB® script to compute the area of a triangle from Python®.
To call a MATLAB script or function, put it on your MATLAB path. For this example, create a MATLAB script in a file named triarea.m
in your current
folder.
b = 5; h = 3; a = 0.5*[b.* h]
After you save the file, start Python and call the script.
import matlab.engine eng = matlab.engine.start_matlab[] eng.triarea[nargout=0]
Specify nargout=0
. Although the script prints output, it returns no output arguments to Python.
Convert the script to a function and call the function from the engine. To edit the file, open the MATLAB editor.
eng.edit['triarea',nargout=0]
Delete the three statements. Then add a function declaration and save the file.
function a = triarea[b,h]
a = 0.5*[b.* h];
Call the new triarea
function from the
engine.
ret = eng.triarea[1.0,5.0] print[ret]
The triarea
function returns only one output argument, so there is no need to specify nargout
.
See Also
matlab.engine.MatlabEngine
|
matlab.engine.FutureResult
Related Topics
- Call MATLAB Functions from Python
I'm new to Python and I'm needing to convert some Matlab code I have to Python. Here's the code I have:
save[myFile, 'list','config'];
else
load[myfile, 'list'];
end
I've been looking within Python and I can't seem to find an equivalent method for this. Any help would be greatly appreciated.
Thanks!
- python
- matlab
asked Oct 25, 2012 at 20:23
shadonarshadonar
1,0943 gold badges15 silver badges38 bronze badges
1
1 Answer
Checkout the numpy IO routines here, they can handle various formats.
answered Oct 25, 2012 at 20:27
BitwiseBitwise
7,3115 gold badges32 silver badges50 bronze badges
0