Create a Python Application with MATLAB Code

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:

  1. In MATLAB, examine the MATLAB code that you want to deploy as a Python package.

    1. Open makesqr.m.

      function y = makesqr(x)
      
      y = magic(x);
      
    2. At the MATLAB command prompt, enter makesqr(5).

      The output appears as follows:

      ans =
      
          17    24     1     8    15
          23     5     7    14    16
           4     6    13    20    22
          10    12    19    21     3
          11    18    25     2     9
  2. Open the Library Compiler app.

    1. On the toolstrip, select the Apps tab.

    2. Click the arrow at the far right of the tab to open the apps gallery.

    3. Click Library Compiler.

  3. In the Application Type section of the toolstrip, select Python Package from the list.

  4. Specify the MATLAB functions you want to deploy.

    1. In the Exported Functions section of the toolstrip, click the plus button.

    2. In the file explorer that opens, locate and select the makesqr.m file.

      makesqr.m is located in matlabroot\toolbox\javabuilder\Examples\MagicSqaureExample\MagicDemoComp.

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

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

  6. Click Package.

  7. Select the Open output folder when process completes check box.

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

  9. Click Close on the Package window.

  10. Open a command prompt in the for_redistribution_files_only folder.

  11. Run the set up script to install the package.

    python setup.py install
  12. Create a new file called getmagic.py.

  13. Using a text editor, open getmagic.py.

  14. Paste the following code into the file.

    import makesqr
    import matlab
    
    myMagic = makesqr.initialize()
    
    myMagic.makesqr(5)
    
    myMagic.terminate()
    
  15. From the system's command prompt, run the application.

    python getmagic.py
    matlab.double([[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 Mac OS X you must use the mwpython script. The mwpython script is located in the matlabroot/bin folder. matlabroot is the location of your MATLAB installation. For example:

      mwpython getmagic.py

See Also

Was this topic helpful?