Main Content

Package MATLAB Function and Deploy to Python Application


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

1. Save the following code in a sample file named makesqrSample1.m:

x = 5;
y = makesqr(x);

2. Build the Python package using the 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 ='makesqr.m',...

You can specify additional options in the command by using name-value arguments. For details, see

The 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\ — 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 by pip to build the package. For details, see

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

  • — 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 from the samples folder into the folder that contains the file. The program listing for 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

    my_MagicSquarePkg = MagicSquarePkg.initialize()
except Exception as e:
    print('Error initializing MagicSquarePkg package\\n:{}'.format(e))

    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))


2. At the system command prompt, navigate to the folder that contains and

3. Install the application using the Python command python 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 If you used sample MATLAB code in the packaging steps, this application returns the same output as the sample code.



Note: On macOS, you must use the mwpython script instead of python. For example, mwpython

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

See Also

| | | |

Related Topics