Main Content

Create Standalone Application from MATLAB

Supported Platform: Windows®, Linux®, macOS

This example shows how to use MATLAB® Compiler™ to package the pre-written 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.

You can create standalone applications using the following options:

OptionPurpose
Application Compiler

Use this app to produce an installer that installs both the standalone application and all required dependencies on a target system.

compiler.build.standaloneApplication

Use this function to produce a standalone application that does not include MATLAB Runtime or an installer. To produce a standalone application that does not launch a Windows command shell, use compiler.build.standaloneWindowsApplication.

compiler.package.installerUse this function to produce an installer that installs both the standalone application and all required dependencies on a target system.
mccUse this function to produce a standalone application that does not include MATLAB Runtime or an installer.

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.

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 Application Compiler App

  1. On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Application Compiler.

    Alternately, you can open the Application Compiler app by entering applicationCompiler at the MATLAB prompt.

  2. In the MATLAB Compiler project window, specify the main file of the MATLAB application that you want to deploy.

    1. In the Main File section of the toolstrip, click .

    2. In the Add Files window, browse to matlabroot\extern\examples\compiler, and select magicsquare.m. Click Open.

      The function magicsquare.m is added to the list of main files.

  3. Decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the two options in the Packaging Options section:

    • Runtime downloaded from web — Generates an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application.

    • Runtime included in package — Generates an installer that includes the MATLAB Runtime installer.

  4. Customize the packaged application and its appearance:

    • Application information — Editable information about the deployed application. You can also customize the standalone applications appearance by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.

    • Command line input type options — Selection of input data types for the standalone application. For more information, see Determine Data Type of Command-Line Input (For Packaging Standalone Applications Only).

    • Additional installer options — Edit the default installation path for the generated installer and selecting custom logo. See Change the Installation Path .

    • Files required for your application to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.

    • Files installed for your end user — Files that are installed with your application. These files include:

      • Generated readme.txt

      • Generated executable for the target platform

      See Specify Files to Install with Application.

    • Additional runtime settings — Platform-specific options for controlling the generated executable. See Additional Runtime Settings.

      Caution

      On Windows operating systems, when creating a console only application, uncheck the box Do not display the Windows Command Shell (console) for execution. By default, this box is checked. If the box is checked, output from your console only application is not displayed. Since this example is a console only application, the box must be unchecked.

  5. To generate the packaged application, click Package.

    In the Save Project dialog box, specify the location to save the project.

  6. In the Package dialog box, verify that Open output folder when process completes is selected.

    When the packaging process is complete, examine the generated output.

    • Three folders are generated in the target folder location: for_redistribution, for_redistribution_files_only, and for_testing.

      For further information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.

    • PackagingLog.html — Log file generated by MATLAB Compiler.

  7. To install your standalone application, see Install Standalone Application.

Create Standalone Application Using compiler.build.standaloneApplication

Note

If you have already created a standalone application using the Application Compiler app, you can skip this section. However, if you want to know how to create a standalone application from the MATLAB command window using a programmatic approach, follow these instructions.

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

    appFile = fullfile(matlabroot,'extern','examples','compiler','magicsquare.m');
  2. Build the standalone application using the compiler.build.standaloneApplication function.

    buildResults = compiler.build.standaloneApplication(appFile);

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

  3. The function generates the following files within a folder named magicsquarestandaloneApplication in your current working directory:

    • magicsquare.exe or magicsquare — Executable file that has the .exe extension if compiled on a Windows system, or no extension if compiled on Linux or macOS systems.

    • run_magicsquare.sh — Shell script file that sets the library path and executes the application. This file is only generated on Linux and macOS systems.

    • mccExcludedFiles.log — Log file that contains a list of any toolbox functions that were not included in the application. For information on non-supported functions, see MATLAB Compiler Limitations.

    • readme.txt — Text file that contains information on deployment prerequisites and the list of files to package for deployment.

    • requiredMCRProducts.txt — Text file that contains product IDs of products required by MATLAB Runtime to run the application.

    • unresolvedSymbols.txt — Text file that contains information on unresolved symbols.

    Note

    The generated standalone executable does not include MATLAB Runtime or an installer.

  4. Additional options can be specified by using one or more comma-separated pairs of name-value arguments in the compiler.build command.

    • 'AdditionalFiles' — Paths to additional files to include in the standalone application.

    • 'AutoDetectDataFiles' — Flag to automatically include data files.

    • 'CustomHelpTextFile' — Path to a file containing help text for the end user of the application.

    • 'EmbedArchive' — Flag to embed the standalone archive in the generated executable.

    • 'ExecutableIcon' — Path to a custom icon image.

    • 'ExecutableName' — Name of the generated application.

    • 'ExecutableSplashScreen' — Path to a custom splash screen image. This option is only used when you compile using the compiler.build.standaloneWindowsApplication function.

    • 'ExecutableVersion' — System level version of the generated application. This is only used on Windows systems.

    • 'OutputDirectory' — Path to the output directory that contains generated files.

    • 'TreatInputsAsNumeric' — Flag to interpret command line inputs as numeric MATLAB doubles.

    • 'Verbose' — Flag to display progress information indicating compiler output during the build process.

    For example, you can specify the executable name and enable verbose output.

    buildResults = compiler.build.standaloneApplication(appFile,...
        'ExecutableName','MyMagic','Verbose','On');
  5. To run 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

  6. To run your standalone application outside of MATLAB, see Run Standalone Application.

Create Standalone Application Installer Using the compiler.package.installer Function

Note

If you have already created a standalone application installer using the Application Compiler app, you can skip this section. However, if you want to know how to create an installer for a standalone application from the MATLAB command prompt using a programmatic approach, follow these instructions.

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

    appFile = fullfile(matlabroot,'extern','examples','compiler','magicsquare.m');
  2. Build the standalone application using the compiler.build.standaloneApplication function and save the output in a compiler.build.Results object.

    buildResults = compiler.build.standaloneApplication(appFile);
  3. Create an installer by using the Results object buildResults as an input argument to the compiler.package.installer function.

    compiler.package.installer(buildResults);

    The function creates a new folder that contains the standalone application installer.

  4. Additional options can be specified as one or more comma-separated pairs of name-value arguments in the compiler.package.installer command.

    • 'ApplicationName' — Name of the installed application.

    • 'AuthorCompany' — Name of company that created the application.

    • 'AuthorEmail' — Email address of the application author.

    • 'AuthorName' — Name of the application author.

    • 'DefaultInstallationDir' — Default directory in which the application is installed.

    • 'Description' — Detailed description of the application.

    • 'InstallationNotes' — Notes about additional requirements for using the application.

    • 'InstallerIcon' — Path to an image file used as the installer's icon.

    • 'InstallerLogo' — Path to an image file used as the installer's logo.

    • 'InstallerName' — Name of the generated installer.

    • 'InstallerSplash' — Path to an image file used as the installer splash screen.

    • 'OutputDir' — Path to the folder that contains the generated installer.

    • 'RuntimeDelivery' — Method to install MATLAB Runtime, specified as one of the following:

      • 'web' (default) — Option for installer to download MATLAB Runtime during application installation.

      • 'installer' — Option to include MATLAB Runtime within the installer so that it can be installed without connecting to the Internet.

    • 'Shortcut' — Path to a file or folder that the installer will create a shortcut to at install time. This is only used on Windows.

    • 'Summary' — Summary of the application.

    • 'Version' — Version number of the generated application.

    For example, you can specify the installer name and include MATLAB Runtime in the installer.

    compiler.package.installer(buildResults,...
        'InstallerName','MyMagic_Install','RuntimeDelivery','installer');

Install Standalone Application

  • To install your application using an installer created by the Application Compiler app or the compiler.package.installer function, see Install Deployed Application.

Run Standalone Application

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

  2. 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 Path for Run-Time Deployment.

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

  4. 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
  5. To create a command line shortcut for the application on Linux or macOS, use the alias command.

    alias mymagic='/path/to/run_magicsquare.sh <MATLAB_RUNTIME_INSTALL_DIR>'

    To run your application with the input argument 4, type mymagic 4 in the terminal.

  6. To make an alias permanent, append the command to the file ~/.bash_aliases in a Bash shell or ~/.zshrc in a Zsh shell.

    echo "alias mymagic='~/MATLAB/apps/run_magicsquare.sh /usr/local/MATLAB/MATLAB_Runtime/v910'" >> ~\.bash_aliases

See Also

| | | | |

Related Topics