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.
To compile a Python package from MATLAB code, follow the following steps one by one.
-
In MATLAB, examine the MATLAB code that you want to deploy as a Python package. The example used here is makesqr.m, located in matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp
a. Open makesqr.m.
function y = makesqr(x) y = magic(x);
b. At the MATLAB command prompt, enter addition(67, 3).
The output appears as follows:
ans = 8 1 6 3 5 7 4 9 2
-
Open the Library Compiler app.
a. On the toolstrip, select the Apps tab.
b. Click the arrow at the far right of the tab to open the apps gallery.
c. Click Library Compiler.
-
In the Application Type section of the toolstrip, select Python Package from the list.
-
Specify the MATLAB functions you want to deploy.
a. In the Exported Functions section of the toolstrip, click the plus button.
b. In the file explorer that opens, locate and select the makesqr.m file.
makesqr.m is located in matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp.
c. Click Open to select the file, and close the file explorer.
makesqr.m is added to the list of exported files and a minus button appears under the plus button. In addition, makesqr is set as the package name.
-
In the top field of Application Information, replace Library Name makesqr with MagicSquarePkg.
For more information on naming requirement for the Python package, see Import Compiled Python Packages.
-
In the Packaging Options section of the toolstrip, verify that the Runtime downloaded from web check box is selected.
This option creates an application installer that automatically downloads the MATLAB Runtime and installs it along with the deployed package.
-
Click Save to specify a project name and save the project.
-
Click Package.
-
Select the Open output folder when process completes check box.
-
Verify that the generated output contains:
-
for_redistribution — A folder containing the installer to distribute the package
-
for_testing — A folder containing the raw generated files to create the installer
-
for_redistribution_files_only — A folder containing only the files needed to redistribute the package
-
PackagingLog.txt — A log file generated by the compiler
-
-
Click Close on the Package window.
-
Open a command prompt in the for_redistribution_files_only folder.
-
Run the setup script to install the package.
python setup.py install
-
Create a new file called getmagic.py. Paste the following code into the file.
import MagicSquarePkg myMagic = MagicSquarePkg.initialize() print(myMagic.makesqr(3)) myMagic.terminate()
-
From the system's command prompt, run the application.
python getmagic.py
The following output will be displayed:
[[8.0,1.0,6.0], [3.0,5.0,7.0], [4.0,9.0,2.0]]
-
Visit http://ompc.juricap.com/download and download any of the archives zip, tar.gz or tar.bz2 file
OR
You can also download by clicking on the links given below.
i. gip - https://www.bitbucket.org/juricap/ompc/get/tip.zip
ii. gz - https://www.bitbucket.org/juricap/ompc/get/tip.gz
iii. bz2 - https://www.bitbucket.org/juricap/ompc/get/tip.bz2
-
Suppose we downloaded zip, it will have the name juricap-ompc-96c520b01abc.zip, double click on it to extract then you will get new directory named juricap-ompc-96c520b01abc.
-
Now, rename juricap-ompc-96c520b01abc directory to any name, suppose we renamed it to matlab-ompc-trial. Navigate inside the matlab-ompc-trial directoy from command terminal.
MacBook-Pro-2:matlab-ompc-trial admin$ cd ~/Downloads/ MacBook-Pro-2:Downloads admin$ MacBook-Pro-2:Downloads admin$ mv juricap-ompc-96c520b01abc matlab-ompc-trail MacBook-Pro-2:Downloads admin$ MacBook-Pro-2:Downloads admin$ cd matlab-ompc-trail/ MacBook-Pro-2:matlab-ompc-trail admin$
-
You will be able to see the following contents inside this directory.
MacBook-Pro-2:matlab-ompc-trial admin$ ls LICENSE examples ompc ompceg sandbox test.py README licenses ompc.cfg ompclib setup.py MacBook-Pro-2:matlab-ompc-trial admin$
-
Place the matlab-ompc-trial directory in a proper place in your system. Suppose we moved/copied/placed matlab-ompc-trial on ~/Desktop from Terminal. You can use GUI also.
MacBook-Pro-2:Downloads admin$ mv matlab-ompc-trial/ ~/Desktop/ MacBook-Pro-2:Downloads admin$ MacBook-Pro-2:Downloads admin$ cd ~/Desktop/ MacBook-Pro-2:Desktop admin$ MacBook-Pro-2:Desktop admin$ cd matlab-ompc-trial/ MacBook-Pro-2:matlab-ompc-trial admin$
-
Create a file named box_volume.m and paste the following shadowed code.
box_volume.m
% ********************************************** % created on : 6 Jan 2018 % aim of code : To calculate volume of a square % coded by : Rishikesh Agrawani % ********************************************** % Function that calculates base area of a box function area = box_area(length, width) area = length * width; end % Function that alculates volume of a box, it calls box_area() to get base area function volume = box_volume(length, width, height) volume = box_area(length, width) * height end
-
Now, create python file named box_volume_test.py in the same directory and paste the following code
box_volume_test.py
import ompc addpath('.') import box_volume v = box_volume(1, 2, 5); v1 = box_volume(1, 2, 3) v2 = box_volume(12, 5, 10)
-
Finally run the python file from terminal using python box_volume_test.py. You will be able to see the following on terminal.
MacBook-Pro-2:matlab-ompc-trial admin$ python box_volume_test.py /Users/admin/Desktop/matlab-ompc-trial/ompc/byteplay.py:43: UserWarning: byteplay supports only Python versions 2.4 and 2.5 warnings.warn('byteplay supports only Python versions 2.4 and 2.5') An "end" without matching keyword! On line: 10! An "end" without matching keyword! On line: 15! Importing m-file: "/Users/admin/Desktop/matlab-ompc-trial/box_volume.m" ans = 10.0 ans = 6.0 ans = 600.0
Does it support conversion of matlab classes?
Does it support conversion of matlab table?