main content

create standalone application from matlab function -凯发k8网页登录

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)
    n=str2double(n);
end
m = magic(n); 
disp(m)

test the function at the command prompt.

magicsquare(5)
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

use the compiler.build.standaloneapplication function to create a standalone application from the matlab function.

appfile = "magicsquare.m";
buildresults = compiler.build.standaloneapplication(appfile);

you can specify additional options in the compiler.build 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.

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

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

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:

windows

!magicsquare 4

linux

!./magicsquare 4

macos

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

to run your standalone application outside of the matlab desktop environment, see .

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.

compiler.package.installer(buildresults);

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 .

for example, to specify the installer name and include matlab runtime in the installer, execute:

compiler.package.installer(buildresults, ...
'installername','mymagic_install','runtimedelivery','installer');

install standalone application

to install your application using an installer created by the function, see .

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:

windows

magicsquare 5

linux

using the shell script:

./run_magicsquare.sh  5

using the executable:

./magicsquare 5

macos

using the shell script:

./run_magicsquare.sh  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 details, see .

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/run_magicsquare.sh '

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/run_magicsquare.sh /usr/local/matlab/matlab_runtime/r2023a'" >> ~/.bash_aliases

tips

  • instead of using the function to create a standalone application, you can also use the application compiler app to create a standalone application.

  • to produce a standalone application that does not launch a windows command shell, use .

  • you can also use the command to produce a standalone application that does not include matlab runtime or an installer.

see also

| | | | |

related topics

网站地图