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 of a computer. The target system does not require a licensed copy of MATLAB to run the application.
You can create standalone applications using any of the following options::
Use the Application Compiler app. Using this option produces an installer that installs both the standalone application and all required dependencies on a target system.
Use the compiler.build.standaloneApplication
function. This function
produces a standalone executable that does not include MATLAB Runtime or an installer. To package the files and create an installer, use
compiler.package.installer
.
Use the mcc
command. This command
produces a standalone executable that does not include MATLAB Runtime or an installer. To package the files and create an installer, use
compiler.package.installer
.
Note
The file extension varies depending on the platform on which the installer was generated.
In MATLAB, examine the MATLAB code that you want deployed as a standalone application. For this example,
open magicsquare.m
located in
.matlabroot
\extern\examples\compiler
function m = magicsquare(n) if ischar(n) n=str2double(n); end m = magic(n)
At the MATLAB command prompt, 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
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.
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 left 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.
compiler.build.standaloneApplication
FunctionNote
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 prompt using a programmatic approach, follow these instructions.
Build the standalone application using the compiler.build.standaloneApplication
function.
appFile = fullfile(matlabroot,'extern','examples','compiler','magicsquare.m'); buildResults = compiler.build.standaloneApplication(appFile);
Information about the build type, included files, and build options are saved
to the compiler.build.Results
object
buildResults
.
The following files are generated within a folder named
magicsquarestandaloneApplication
in your current working
directory:
magicsquare.exe
or
mymagic.sh
—Executable file that has the
.exe
extension if compiled on a Windows system or the .sh
extension if
compiled on Linux or macOS.
mccExcludedFiles.log
—Log file that contains a
list of any toolbox functions that were not included in the
application. For more information on non-supported functions, see
MATLAB Compiler
Limitations.
readme.txt
—Readme 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.
Note
This method does not produce an installer.
Additional options can be specified as one or more comma-separated pairs of
name-value arguments in the compiler.build
command.
'AdditionalFiles'
— Path to additional files to be
included in the standalone application.
'AutoDetectDataFiles'
— Flag to automatically
include data files.
'CustomHelpTextFile'
— Path to a help 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.
'ExecutableVersion'
— System-level version of
the generated application. This is only used on Windows.
'OutputDirectory'
— Path to the output
directory where the build files are saved.
'TreatInputsAsNumeric'
— Flag to interpret
command line inputs as numeric MATLAB doubles.
'Verbose'
— Flag to display progress
information indicating compiler output during the build process.
appFile = fullfile(matlabroot,'extern','examples','compiler','magicsquare.m'); buildResults = compiler.build.standaloneApplication(appFile,... 'ExecutableVersion','2.0','Verbose','On');
Create an installer by passing the compiler.build.Results
object buildResults
as an input argument to the compiler.package.installer
function.
compiler.package.installer(buildResults)
This creates a new folder containing the installer.
If you created an installer using the Application Compiler App, you can
install the standalone application by double-clicking the
MyAppInstaller_web
executable in the
for_redistribution
folder.
Note
The file extension varies depending on the platform on which the installer was generated.
If you want to connect to the Internet using a proxy server, click Connection Settings. Enter the proxy server settings in the provided window. Click OK.
To complete installation, follow the instructions on the user interface.
Note
On Linux and Mac OS X, you do not have the option of adding a desktop shortcut.
To run your standalone application:
Open a terminal window.
Navigate to the folder into which you installed the application.
If you accepted the default settings, you can find the folder in one of the following locations:
Windows | C:\Program
Files\magicsquare |
macOS | /Applications/magicsquare |
Linux | /usr/magicsquare |
Run the application using one of the following commands:
Windows | application\magicsquare 5 |
macOS | First, set the
Now run the application:
|
Linux | ./ magicsquare 5 |
A 5-by-5 magic square is displayed 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
applicationCompiler
| compiler.build.standaloneApplication
| compiler.build.standaloneWindowsApplication
| compiler.package.installer
| deploytool
| mcc