Tutorial: Using Matlab compiled classes from within Python

Unlike other Matlab features, it seems that using Matlab compiler for creating a Python package is poorly documented. It took me quite some time, mostly by trial and error, to understand how to use Matlab classes (classdef) from within Python.

In this tutorial we will discuss the following basic Matlab class:

Modifying the class for efficient work from python

We inherit our class from “handle”  (Handle classes define objects that reference the object data. Copying an object creates another reference to the same data.)

Notice new propValue / setPropValue methods – it is used for accessing public properties from Python (as long as I am aware, there is no similar built-in functions within auto-generated Python package.

Python example

Compiling the package

Installing python package

  • Run generated installer package and follow instruction:
  • Install the runtime into /home/user/MATLAB/MATLAB_Runtime
  • Install the application into /home/user/MyPackage
  • Install Python package:

Running python example (Linux)

Linux execution requires proper LD_LIBRARY_PATH definition. Assuming our Matlab runtime is installed in /home/user/MATLAB/MATLAB_Runtime, we need to define it as the following:

Execute Python example

Loading external MAT file within Matlab package

To successfully load the .mat file it needs to be placed within “Files required for your library to run” section within MATLAB Compiler project

Leave a Reply

Your email address will not be published. Required fields are marked *