This example shows how to transform a MATLAB® function
into a .NET assembly. The example compiles a MATLAB function, makesquare
,
which computes a magic square, into a .NET assembly named MagicSquareComp
,
which contains the MLTestClass
class and other
files needed to deploy your application.
In MATLAB, create the function that you want to deploy as a shared library.
This example uses the sample function, called makesquare.m
,
included in the
folder,
where matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NET\MagicSquareExample\MagicSquareCompmatlabroot
represents the name of
your MATLAB installation folder.
function y = makesquare(x)
y = magic(x);
Run the example in MATLAB.
makesquare(5)
17241815235714164613202210121921311182529
Open the Library Compiler app.
On the toolstrip, select the Apps tab.
Click the arrow at the far right of the tab to open the apps gallery.
Click Library Compiler.
Note:
You can also call the |
In the Application Type section of the toolstrip, select .NET Assembly from the list.
Note: If the Application Type section of the toolstrip is collapsed, you can expand it by clicking the down arrow. |
Specify the MATLAB functions that you want to deploy.
In the Exported Functions section of the toolstrip, click the plus button.
Note: If the Exported Functions section of the toolstrip is collapsed, you can expand it by clicking the down arrow. |
In the file explorer that opens, locate and select the makesquare.m
file.
Click Open to select the file and close the file explorer.
The Library Compiler app adds makesquare.m to
the list of files and a minus button appears under the plus button.
The Library Compiler app uses the name of the file as the
name of the deployment project file (.prj
), shown
in the title bar, and as the name of the assembly, shown in the first
field of the Application Information area. The project file saves
all of the deployment settings so that you can re-open the project.
In the Packaging Options section of the toolstrip, verify that the Runtime downloaded from web check box is selected.
Note: If the Packaging Options section of the toolstrip is collapsed, you can expand it by clicking the down arrow. |
This option creates an application installer that automatically downloads the MATLAB Runtime and installs it along with the deployed add-in.
Explore the main body of the project window.
The project window is divided into the following areas:
Application Information — Editable information about the deployed application. This information is used by the generated installer to populate the installed application's metadata.
Additional Installer Options — The default installation path for the generated installer.
Files required for your application — Additional files required by the generated application. These files will be included in the generated application installer.
Files installed with your application — Files that are installed with your application. These files include a DLL and a readme text file.
Additional Runtime Settings — Access to other compiler options.
In the top field of Application Information,
replace makesquare
with MagicSquareComp
.
This specifies the name of the generated installer.
In the Namespace field at the top
of the class browser, replace makesquare
with MagicSquareComp
.
This specifies the name of the generated assembly and the namespace into which the class is placed.
The namespace for the generated class is the entire string entered into the field.
The assembly name is the last item in the dot (.
)
separated list of names.
For example if you enter foo.bar.demo
, the
generated class is in the namespace foo.bar.demo
and
the generated assembly name is demo
.
In the Class Name column of the
class browser, replace Class1
with MLTestClass
.
Click Package.
The Package window opens while the library is being generated. Select the Open output folder when process completes check box. The packaging process generates a self-extracting file that automatically registers the DLL and unpacks all deployable deliverables.
When the deployment process is complete, a file explorer opens and displays the generated output.
It should contain:
for_redistribution
— A
folder containing the installer to distribute the generated assembly
for_testing
— A folder
containing the raw files generated by the compiler
for_redistribution_files_only
—
A folder containing only the files needed to redistribute the assembly
PackagingLog.txt
— A log
file generated by the compiler
Click Close on the Package window.