Main Content

Create Standalone Application from MATLAB

Supported Platforms: Windows®, Linux®, macOS

This example shows how to use MATLAB® Compiler™ to package a function that prints a magic square to the command prompt. The target system does not require a licensed copy of MATLAB to run the application.

Note

The application is not cross platform, and the executable type depends on the platform on which it was generated.

Create Function in MATLAB

In MATLAB, locate the MATLAB code that you want to deploy as a standalone application.

For this example, compile using the file magicsquare.m located in matlabroot\extern\examples\compiler.

copyfile(fullfile(matlabroot,'extern','examples','compiler','magicsquare.m'));
function m = magicsquare(n)

if ischar(n)
    n=str2double(n);
end
m = magic(n);
disp(m)

In the MATLAB command window, enter magicsquare(5);.

The output is:

    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

Create Standalone Application Using compiler.build.standaloneApplication

Build a standalone application using a programmatic approach. Alternatively, if you want to create a standalone application package using a graphical interface, see Create Standalone Application Using Standalone Application Compiler App.

  1. Build the standalone application using the compiler.build.standaloneApplication function.

    buildResults = compiler.build.standaloneApplication("magicsquare.m");

    You can specify additional options in the compiler.build command by using name-value arguments. For details, see compiler.build.standaloneApplication.

    For information on including additional files to your application, such as images, see Include and Access Files in Packaged Applications.

    The compiler.build.Results object buildResults contains information on the build type, generated files, included support packages, and build options.

    The build function generates files within a folder named magicsquarestandaloneApplication in your current working directory. For information on the files generated, see Files Generated After Packaging MATLAB Functions.

    Note

    The generated files do not include an installer for the application or MATLAB Runtime. To create an installer using the buildResults object, see compiler.package.installer.

  2. To test magicsquare from MATLAB with the input argument 4, navigate to the magicsquarestandaloneApplication folder and execute one of the following commands based on your operating system:

    Operating SystemTest in MATLAB Command Window
    Windows!magicsquare 4
    macOSsystem(['./run_magicsquare.sh ',matlabroot,' 4']);
    Linux!./magicsquare 4

Run Standalone Application

  1. In your system command prompt, navigate to the folder containing your standalone executable.

  2. Run magicsquare with the input argument 5 by using one of the following commands based on your operating system:

    Operating SystemCommand
    Windowsmagicsquare 5
    Linux

    Using the shell script:

    ./run_magicsquare.sh <MATLAB_RUNTIME_INSTALL_DIR> 5

    Using the executable:

    ./magicsquare 5

    macOS

    Using the shell script:

    ./run_magicsquare.sh <MATLAB_RUNTIME_INSTALL_DIR> 5

    Using the executable:

    ./magicsquare.app/Contents/macOS/magicsquare 5

    Note

    To run the application without using the shell script on Linux and macOS, you must first add MATLAB Runtime to the library path. For more information, see Set MATLAB Runtime Library Paths for Deployment.

  3. The application outputs a 5-by-5 magic square in the console:

        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

Tips

  • To produce a standalone application that does not launch a Windows command shell, use compiler.build.standaloneWindowsApplication.

  • To specify additional compilation options, you can use the mcc command to create a standalone application that does not include MATLAB Runtime or an installer.

See Also

| | | | |

Topics