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 `matlabroot\extern\examples\compilersdk\python\rectangle\`:

 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

1. 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.

2. Examine the MATLAB function `rectangleDemo.m`.

The function creates two `Point` objects, then creates a `Rectangle` 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 ```

3. Build a Python package named `rectangleLib` using all of the MATLAB function files in the `rectangle` folder.

1. Get the list of files with a `.m` extension in the current directory.

`functionfiles = dir('*.m')`

2. Save the filenames in a cell array.

`functionfiles = {functionfiles.name};`

3. 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.

4. Write source code for a Python application that accesses the MATLAB functions.

The sample application for this example is `rectangleDriver.py` in the `rectangle` folder.

The `rectangleDriver` application:

1. Defines two classes, `PyPoint` and `PyRectangle`

2. Creates a handle to the package using `rectangleLib.initialize`

3. Passes the package handle to the `PyPoint` and `PyRectangle` classes

4. Creates a `PyRectangle` object and prints its area, height, and width

5. Saves each corner as a `PyPoint` object and prints its coordinates

6. Calls the `rectangleDemo` MATLAB function to create and display data from a different rectangle

5. Open a system command prompt window and navigate to the `rectangleLibpythonPackage` folder that contains your generated package.

6. Install the package using the `python` command.

``python -m pip install .``

For more details, see Install and Import MATLAB Compiler SDK Python Packages.

7. 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 of `python`. For example, ```mwpython rectangleDriver.py```.

The `mwpython` script is located in the `matlabroot/bin` folder, where `matlabroot` is the location of your MATLAB or MATLAB Runtime installation.