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:
| Option | Purpose |
|---|---|
| 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.package.installer | Use this function to produce an installer that installs both the standalone application and all required dependencies on a target system. |
mcc | Use 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.
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 9On 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.
In the MATLAB Compiler project window, specify the main file of the MATLAB application that you want to deploy.
In the Main File section of the toolstrip, click
.
In the Add Files window, browse to
,
and select matlabroot\extern\examples\compilermagicsquare.m. Click
Open.
The function magicsquare.m is added to the list of
main files.
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.
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
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.
To generate the packaged application, click Package.
In the Save Project dialog box, specify the location to save the project.
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.
To install your standalone application, see Install Standalone Application.
compiler.build.standaloneApplicationNote
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.
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');
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.
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.
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');
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 System | Test in MATLAB Command Window |
|---|---|
| Windows | !magicsquare 4 |
| macOS | system(['./run_magicsquare.sh ',matlabroot,'
4']); |
| Linux | !./magicsquare 4 |
To run your standalone application outside of MATLAB, see Run Standalone Application.
compiler.package.installer FunctionNote
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.
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');
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);
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.
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');
To install your application using an installer created by the Application
Compiler app or the compiler.package.installer
function, see Install Deployed Application.
In your system command prompt, navigate to the folder containing your standalone executable.
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.
Run magicsquare with the input argument
5 by using one of the following commands based on your
operating system:
| Operating System | Command |
|---|---|
| Windows | magicsquare 5 |
| Linux | Using the shell script:
Using the executable:
|
| macOS | Using the shell script:
Using the executable:
|
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 9To 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.
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_aliasesapplicationCompiler | compiler.build.standaloneApplication | compiler.build.standaloneWindowsApplication | compiler.package.installer | deploytool | mcc