Skip to content

Latest commit

 

History

History
104 lines (62 loc) · 4.73 KB

matlab.md

File metadata and controls

104 lines (62 loc) · 4.73 KB

Run Matlab programs in Python

This example shows how to create a Python package using a MATLAB function. You can then pass the generated package to the developer who is responsible for integrating it into an application. The target system does not require a licensed copy of MATLAB.

Requirements

  • A Matlab version with a license for MATLAB Compiler or MATLAB Compiler SDK to compile the Python package.
  • The target computer (where the final application will run) does not require Matlab or a Matlab Licence. However, it requires the Matlab Runtime (free, download here) of the same version of the Matlab used to compile the application. For instance, if the package was compiled in Matlab 2018a the target computer must have Matlab Runtime version 2018a installed

1) Create a Python package for the Maltab Application

For this example let's create a simple Matlab function and save it as makesqr.m:

function y = makesqr(x)

y = magic(x);

Now lets create the Python package. Enter libraryCompiler at the MATLAB prompt to open the Library Compiler. To use the Library Compiler you need a license for MATLAB Compiler or MATLAB Compiler SDK.

In the MATLAB Compiler project window, specify the main file of the MATLAB application that you want to deploy. Select whether to include the MATLAB Runtime installer in the generated application by selecting one of the two options in the Packaging Options section:

  • Runtime downloaded from web — Generates an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application.

  • Runtime included in package — Generates an application that includes the MATLAB Runtime installer.

Fill in the remaining fields. For this example lets edit the Library Name field, replacing makesqr with MagicSquarePkg.

To generate the packaged application, click Package. When the deployment process is complete, the output folder contains:

  • for_redistribution — Folder containing the file that installs the application and the MATLAB Runtime.

  • for_testing — Folder containing all the artifacts created by mcc, such as binaries and JAR, header, and source files for a specific target. Use these files to test the installation.

  • for_redistribution_files_only — Folder containing the files required for redistributing the application. Distribute these files to users who have MATLAB or MATLAB Runtime installed on their machines.

  • PackagingLog.txt — Log file generated by MATLAB Compiler.

2) Set up the target computer (Ubuntu server)

  1. Make sure Python is installed. Python versions supported are versions 2.7, 3.5, and 3.6. For 64-bit MATLAB, the 64-bit Python version is requiered.

  2. If necessary install setuptools for python: sudo apt-get install python-setuptools

  3. Now we need to install the Matlab Runtime in the Ubuntu server. Download the it from the website at Mathworks. Make sure you download and install the version corresponding to the Matlab you used to create the Python package.

  4. Start the MATLAB Runtime installer. On Ubuntu: sudo ./install On Debian: gksudo ./install

  5. Follow the installation dialog instructions.

Install the MATLAB Runtime Non-Interactively

To install the MATLAB Runtime without having to interact with the installer dialog boxes (headless Ubuntu server for instance), use the MATLAB Runtime installer’s non-interactive silent mode. The installer runs as a background task and does not display any dialog boxes. Extract the contents of the MATLAB Runtime installer file to a temporary folder and run the MATLAB Runtime installer, specifying the -mode option:

./install -mode silent -agreeToLicense yes

3) Create and install the Python application that will use the Matlab function

To install the Python application, open a command prompt in the for_redistribution_files_only folder and run the script:

python setup.py install

Create a file called getmagic.py, and include the following code in it.

import MagicSquarePkg

myMagic = MagicSquarePkg.initialize()
print(myMagic.makesqr(3))
myMagic.terminate()

At the system command prompt, navigate to the folder containing getmagic.py and run the application as follows:

python getmagic.py

Notes

Uninstall MATLAB Runtime

Exit the application and run the command:

rm -rf mcr_root

where mcr_root represents the name of your top-level MATLAB installation folder.

References