Main Content

Create Standalone Application from MATLAB Function

This example shows how to use MATLAB® Compiler™ to package a MATLAB® function into a standalone application. The target system only requires a MATLAB Runtime installation to run the application and does not require a licensed copy of MATLAB.

Supported Platforms: Windows®, Linux®, macOS

Create Function in MATLAB

Write a MATLAB function named magicsquare that prints a magic square matrix at the command prompt. Save the function in a file named magicsquare.m.

function m = magicsquare(n) 
if ischar(n)
m = magic(n); 

Test the function at the command prompt.

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

Use the function to create a standalone application from the MATLAB function.

appFile = "magicsquare.m";
buildResults =;

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

The buildResults object contains information on the build type, generated files, included support packages, and build options. For details, see

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

  • includedSupportPackages.txt — Text file that lists all support files included in the application.

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

  • — 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 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. To create an installer using the buildResults object, see Create Standalone Application Installer Using compiler.package.installer.

Test Standalone Application

To run magicsquare from within MATLAB with the input argument 4, navigate to the magicsquarestandaloneApplication folder from within the MATLAB desktop environment and execute one of the following commands based on your operating system:


!magicsquare 4


!./magicsquare 4


system(['./ ',matlabroot,' 4']);

To run your standalone application outside of the MATLAB desktop environment, see Run Standalone Application.

Create Standalone Application Installer Using compiler.package.installer

Create an installer using the buildResults object as an input argument to the compiler.package.installer function.


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

By default, the installer is configured to download MATLAB Runtime from the web. You can modify this and specify additional options by using name-value arguments. For details, see compiler.package.installer.

For example, to specify the installer name and include MATLAB Runtime in the installer, execute:

compiler.package.installer(buildResults, ...

Install Standalone Application

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

Run Standalone Application

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

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


magicsquare 5


Using the shell script:


NOTE: On Linux, the application uses software OpenGL™ by default. You can force hardware OpenGL by removing the path ${MCRROOT/sys/opengl/lib/glnxa64 from the shell script.

Using the executable:

./magicsquare 5


Using the shell script:


Using the executable:

./ 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 details, see Set MATLAB Runtime Path for Deployment.

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

To create a command line shortcut for the application on Linux or macOS, use the alias command.

alias mymagic='/path/to/ <MATLAB_RUNTIME_INSTALL_DIR>'

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

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

echo "alias mymagic='~/MATLAB/apps/ /usr/local/MATLAB/MATLAB_Runtime/R2023a'" >> ~/.bash_aliases


See Also

| | | | |

Related Topics