Create Python Application with Multiple MATLAB Functions
Supported platforms: Windows®, Linux®, Mac
This example shows how to create a Python® application that uses multiple MATLAB® functions to compute data from a rectangle.
Prerequisites
Verify that you have a version of Python installed that is compatible with MATLAB Compiler SDK™. For details, see MATLAB Supported Interfaces to Other Languages.
This example uses the following files in
:
\extern\examples\compilersdk\python\rectangle\matlabroot
MATLAB Functions | getPointCoordinates.m
getRectangleArea.m getRectangleCorners.m
getRectangleHeight.m getRectangleWidth.m makePoint.m makeRectangle.m Point.m Rectangle.m rectangleDemo.m |
Python Application Code | rectangleDriver.py |
Create and Run Python Application
At the MATLAB command prompt, copy the
rectangle
folder that ships with MATLAB to your work folder.copyfile(fullfile(matlabroot,"extern","examples", ... "compilersdk","python","rectangle"),"rectangle");
Navigate to the new
rectangle
folder in your work folder.Examine the MATLAB function
rectangleDemo.m
.The function creates two
Point
objects, then creates aRectangle
object using the points as corners. It then calculates and displays data about the rectangle.function rectangleDemo() % RECTANGLEDEMO Construct a rectangle and print information about it pointZeroZero = makePoint(0, 0); pointThreeFour = makePoint(3, 4); rectA = makeRectangle(pointZeroZero, pointThreeFour); corners = getRectangleCorners(rectA); showPointCoordinates(corners.upperLeft, 'Upper left-hand corner'); showPointCoordinates(corners.lowerLeft, 'Lower left-hand corner'); showPointCoordinates(corners.upperRight, 'Upper right-hand corner'); showPointCoordinates(corners.lowerRight, 'Lower right-hand corner'); fprintf('Area: %.1f\n', area(rectA)); fprintf('Height: %.1f\n', height(rectA)); fprintf('Width: %.1f\n', width(rectA)); end % This is an auxiliary function. It cannot be called outside rectangleDemo(). function showPointCoordinates(pt, desc) coordinates = getPointCoordinates(pt); fprintf('%s: (%.1f, %.1f)\n', desc, coordinates.X, coordinates.Y); end
Build a Python package named
rectangleLib
using all of the MATLAB function files in therectangle
folder.Get the list of files with a
.m
extension in the current directory.functionfiles = dir('*.m')
Save the filenames in a cell array.
functionfiles = {functionfiles.name};
Compile the Python package using
compiler.build.pythonPackage
.buildResults = compiler.build.pythonPackage(functionfiles,'PackageName','rectangleLib');
For more details, see Generate Python Package and Build Python Application.
Write source code for a Python application that accesses the MATLAB functions.
The sample application for this example is
rectangleDriver.py
in therectangle
folder.The
rectangleDriver
application:Defines two classes,
PyPoint
andPyRectangle
Creates a handle to the package using
rectangleLib.initialize
Passes the package handle to the
PyPoint
andPyRectangle
classesCreates a
PyRectangle
object and prints its area, height, and widthSaves each corner as a
PyPoint
object and prints its coordinatesCalls the
rectangleDemo
MATLAB function to create and display data from a different rectangle
Open a system command prompt window and navigate to the
rectangleLibpythonPackage
folder that contains your generated package.Install the package using the
python
command.python -m pip install .
For more details, see Install and Import MATLAB Compiler SDK Python Packages.
Navigate up one directory and run the
rectangleDriver.py
application.cd .. python rectangleDriver.py
Initializing module and creating a rectangle with corners (-1, 2) and (5, 10)... Area: 48.0 Height: 8.0 Width: 6.0 Corners: upperLeft: (-1.0, 2.0) lowerLeft: (-1.0, 10.0) upperRight: (5.0, 2.0) lowerRight: (5.0, 10.0) Executing rectangleDemo... Upper left-hand corner: (0.0, 0.0) Lower left-hand corner: (0.0, 4.0) Upper right-hand corner: (3.0, 0.0) Lower right-hand corner: (3.0, 4.0) Area: 12.0 Height: 4.0 Width: 3.0
Note
On macOS, you must use the
mwpython
script instead ofpython
. For example,mwpython rectangleDriver.py
.The
mwpython
script is located in the
folder, wherematlabroot
/binmatlabroot
is the location of your MATLAB or MATLAB Runtime installation.
See Also
mwpython
| compiler.build.pythonPackage