This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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. The example used here is makesqr.m, located in matlabroot\toolbox\javabuilder\Examples\MagicSquareExample\MagicDemoComp

    1. Open makesqr.m.

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

      The output appears as follows:

      ans =
           8     1     6
           3     5     7
           4     9     2
  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\MagicSquareExample\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 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.

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

  7. Click Save to specify a project name and save the project.

  8. Click Package.

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

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

  11. Click Close on the Package window.

  12. Open a command prompt in the for_redistribution_files_only folder.

  13. Run the setup script to install the package.

    python install
  14. Create a new file called Paste the following code into the file.

    import MagicSquarePkg
    myMagic = MagicSquarePkg.initialize()
  15. From the system's command prompt, run the application.


    The following output will be displayed:



    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:


See Also

Was this topic helpful?