Package MATLAB Function and Deploy to Python Application
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.
End users must have an installation of MATLAB Runtime to run the application. For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime. For details, see Install and Configure MATLAB Runtime.
Create MATLAB Function
In MATLAB, examine the MATLAB code that you want packaged. For this example, create a function named makesqr.m
that contains the following code:
function y = makesqr(x)
y = magic(x);
Create Python Package Using compiler.build.pythonPackage
1. Save the following code in a sample file named makesqrSample1.m
:
x = 5; y = makesqr(x);
2. Build the Python package using the compiler.build.pythonPackage
function and the makesqr.m
file that you wrote earlier. Use name-value arguments to specify the package name and add a sample file.
buildResults = compiler.build.pythonPackage('makesqr.m',... 'PackageName','MagicSquarePkg',... 'SampleGenerationFiles','makesqrSample1.m',... 'Verbose','on');
You can specify additional options in the compiler.build
command by using name-value arguments. For details, see
compiler.build.pythonPackage
.
The compiler.build.Results
object buildResults
contains information on the build type, generated files, included support packages, and build options.
3. The function generates the following files within a folder named MagicSquarePkgpythonPackage
in your current working directory:
samples\makesqrSample1.py
— Python sample application file.GettingStarted.html
— HTML file that contains information on integrating your package.includedSupportPackages.txt
— Text file that lists all support files included in the package.mccExcludedFiles.log
— Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see Limitations.pyproject.toml
— Configuration file that contains build system requirements and information, which are used bypip
to build the package. For details, seepip.pypa.io/en/stable/reference/build-system/pyproject-toml
.readme.txt
— Text file that contains packaging and interface information.requiredMCRProducts.txt
— Text file that contains product IDs of products required by MATLAB Runtime to run the application.setup.py
— Python file that installs the package.unresolvedSymbols.txt
— Text file that contains information on unresolved symbols.
Note: The generated package does not include MATLAB Runtime or an installer. To create an installer using the buildResults
object, see compiler.package.installer
.
Install and Run MATLAB Generated Python Application
After creating your Python package, you can call it from a Python application. This example uses the sample Python code generated during packaging. You can use this sample Python application code as a guide to write your own application.
1. Copy and paste the generated Python file makesqrSample1.py
from the samples
folder into the folder that contains the setup.py
file. The program listing for makesqrSample1.py
is shown below.
#!/usr/bin/env python """ Sample script that uses the MagicSquarePkg package created using MATLAB Compiler SDK. Refer to the MATLAB Compiler SDK documentation for more information. """ import MagicSquarePkg # Import the matlab module only after you have imported # MATLAB Compiler SDK generated Python modules. import matlab try: my_MagicSquarePkg = MagicSquarePkg.initialize() except Exception as e: print('Error initializing MagicSquarePkg package\\n:{}'.format(e)) exit(1) try: xIn = matlab.double([5], size=(1, 1)) yOut = my_MagicSquarePkg.makesqr(xIn) print(yOut, sep='\\n') except Exception as e: print('Error occurred during program execution\\n:{}'.format(e)) my_MagicSquarePkg.terminate()
2. At the system command prompt, navigate to the folder that contains makesqrSample1.py
and setup.py
.
3. Install the application using the Python command python setup.py install
To install to a location other than the default, consult "Installing Python Modules" in the official Python documentation.
4. Run the application at the system command prompt by typing python makesqrSample1.py.
If you used sample MATLAB code in the packaging steps, this application returns the same output as the sample code
[[17.0,24.0,1.0,8.0,15.0],[23.0,5.0,7.0,14.0,16.0],[4.0,6.0,13.0,20.0,22.0],
[10.0,12.0,19.0,21.0,3.0],[11.0,18.0,25.0,2.0,9.0]]
Note:
On macOS, you must use the mwpython
script instead of python
. For example, mwpython makesqrSample1.py
.
The mwpython
script is located in the matlabroot
/bin
folder, where matlabroot
is the location of your MATLAB or MATLAB Runtime installation.
See Also
mwpython
| compiler.build.pythonPackage
| Library Compiler | mcc
| deploytool