Integrate Simple MATLAB Function into .NET Application
Note
The examples for the MATLAB®
Compiler SDK™ product are in
,
where matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NETVS
specifies the version of
Microsoft®
Visual Studio® .NET you are using. You can load projects for all the examples by opening
the following solution in Visual Studio:Version
matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NET\DotNetExamples.sln
The Simple Plot example shows you how to create a .NET assembly that calls a MATLAB function to display a plot. For an example that uses a MATLAB function to modify a structure array, see Phone Book.
In the following examples, you perform these steps to integrate a MATLAB function into a .NET application:
Use the MATLAB Compiler SDK product to convert a MATLAB function to a method of a .NET class and wrap the class in a .NET assembly.
Access the component in either a C# application or a Visual Basic® application by instantiating your .NET class and using the
MWArray
class library to handle data conversion.Build and run the generated application using the Visual Studio .NET development environment.
Prerequisites
Verify that you have met all of the MATLAB Compiler SDK .NET target requirements. For details, see MATLAB Compiler SDK .NET Target Requirements.
Verify that you have Microsoft Visual Studio installed.
End users must have an installation of MATLAB Runtime to run the application. For details, see Download and Install MATLAB Runtime.
For testing purposes, you can use an installation of MATLAB instead of MATLAB Runtime.
Create Simple Plot
Files
MATLAB Function Location |
|
C# Code Location |
|
Visual Basic Code Location |
|
Procedure
Copy the following folder that ships with the MATLAB product to your work folder:
matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PlotExample
At the MATLAB command prompt, navigate to the
PlotExample\PlotComp
subfolder in your work folder.Examine the
drawgraph
function located inPlotExample\PlotComp
.Test the function at the MATLAB command prompt.function drawgraph(coords) plot(coords(1,:), coords(2,:)); pause(5)
x = 0:0.01:10; y = sin(x); z = [x;y]; drawgraph(z)
The function outputs a figure that displays a sine wave.
Build the .NET component with the Library Compiler app or
compiler.build.dotNETAssembly
using the following information:Field Value Library Name PlotComp
Class Name Plotter
File to Compile drawgraph.m
For example, if you are using
compiler.build.dotNETAssembly
, type:buildResults = compiler.build.dotNETAssembly('drawgraph.m', ... 'AssemblyName','PlotComp', ... 'ClassName','Plotter');
For more details, see the instructions in Generate .NET Assembly and Build .NET Application.
Decide whether you are using C# or Visual Basic to access the component.
C#
If you are using C#, write source code for a C# application that accesses the component.
The sample application for this example is in
PlotExample\PlotCSApp\PlotApp.cs
.This statement creates an instance of the
Plotter
class:Plotter plotter= new Plotter();
This statement explicitly casts the native
plotValues
toMWNumericArray
and then calls the methoddrawgraph
:plotter.drawgraph((MWNumericArray)plotValues);
Visual Basic
If you are using Visual Basic, write source code for a Visual Basic application that accesses the component.
The sample application for this example is in
PlotExample\PlotVBApp\PlotApp.vb
.This statement creates an instance of the
Plotter
class:Dim plotter As Plotter = New Plotter
This statement calls the method
drawgraph
:plotter.drawgraph(coords)
In either case, the
PlotApp
program does the following:Creates two arrays of double values.
Creates a
Plotter
object.Calls the
drawgraph
method to plot the equation using the MATLABplot
function.Uses
MWNumericArray
to represent the data needed by thedrawgraph
method to plot the equation.Uses a
try-catch
block to catch and handle any exceptions.
Open the .NET project file that corresponds to your application language using Visual Studio.
C#
If you are using C#, the
PlotCSApp
folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingPlotCSApp.csproj
in Windows® Explorer. You can also open it from the desktop by right-clicking PlotCSApp.csproj and selecting Open Outside MATLAB.Visual Basic
If you are using Visual Basic, the
PlotVBApp
folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingPlotVBApp.vbproj
in Windows Explorer. You can also open it from the desktop by right-clicking PlotVBApp.vbproj and selecting Open Outside MATLAB.
Add a reference to your assembly file
PlotComp.dll
located in the folder where you generated or installed the assembly.Add a reference to the
MWArray
API.If MATLAB is installed on your system matlabroot
\toolbox\dotnetbuilder\bin\win64\<version>
\MWArray.dllIf MATLAB Runtime is installed on your system <MATLAB_RUNTIME_INSTALL_DIR>
\toolbox\dotnetbuilder\bin\win64\<version>
\MWArray.dllBuild and run the
PlotApp
application in Visual Studio .NET.The application displays the following plot:
To follow up on this example:
Try running the generated application on a different computer.
Try building an installer for the package using
compiler.package.installer
.Try integrating an assembly that consists of multiple functions.
Create Phone Book
In this example, you create a .NET assembly that calls a MATLAB function to modify a structure array that contains phone numbers.
Files
MATLAB Function Location |
|
C# Code Location |
|
Visual Basic Code Location |
|
Procedure
Copy the following folder that ships with MATLAB to your work folder:
matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample
At the MATLAB command prompt, navigate to the
PhoneBookExample\PhoneBookComp
subfolder in your work folder.Examine the
makephone
function located inPhoneBookExample\PhoneBookComp
.function book = makephone(friends) book = friends; for i = 1:numel(friends) numberStr = num2str(book(i).phone); book(i).external = ['(508) 555-' numberStr]; end
Test the function at the MATLAB command prompt.
friends(1).name = "Jordan Robert"; friends(1).phone = 3386; friends(2).name = "Mary Smith"; friends(2).phone = 3912; struct2table(makephone(friends))
ans = 2×3 table name phone external _______________ _____ __________________ "Jordan Robert" 3386 {'(508) 555-3386'} "Mary Smith" 3912 {'(508) 555-3912'}
Build the .NET component with the Library Compiler app or
compiler.build.dotNETAssembly
using the following information:Field Value Library Name PhoneBookComp
Class Name PhoneBook
File to Compile makephone
For example, if you are using
compiler.build.dotNETAssembly
, type:buildResults = compiler.build.dotNETAssembly('makephone.m', ... 'AssemblyName','PhoneBookComp', ... 'ClassName','PhoneBook');
For more details, see the instructions in Generate .NET Assembly and Build .NET Application.
Decide whether you are using C# or Visual Basic to access the component.
C#
If you are using C#, write source code for a C# application that accesses the component.
The sample application for this example is in
PhoneBookExample\PhoneBookCSApp\PhoneBookApp.cs
.Visual Basic
If you are using Visual Basic, write source code for a Visual Basic application that accesses the component.
The sample application for this example is in
\PhoneBookExample\PhoneBookVBApp\PhoneBookApp.vb
.
In either case, the
PhoneBookApp
program does the following:Creates a structure array using MWStructArray to represent the example phonebook data containing names and phone numbers.
Instantiates the
Phonebook
class asthePhonebook
object, as shown:
thePhonebook = new phonebook();
Calls the MATLAB function
makephone
to create a modified copy of the structure by adding an additional field, as shown:
result = thePhonebook.makephone(1, friends);
Displays the resulting structure array.
Open the .NET project file that corresponds to your application language using Visual Studio.
C#
If you are using C#, the
PhoneBookCSApp
folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingPhoneBookCSApp.csproj
in Windows Explorer. You can also open it from the desktop by right-clicking PhoneBookCSApp.csproj and selecting Open Outside MATLAB.Visual Basic
If you are using Visual Basic, the
PhoneBookVBApp
folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingPhoneBookVBApp.vbproj
in Windows Explorer. You can also open it from the desktop by right-clicking PhoneBookVBApp.vbproj and selecting Open Outside MATLAB.
Create a reference to your assembly file
PhoneBookComp.dll
located in the folder where you generated the assembly.Create a reference to the
MWArray
API, which is located in:MATLAB matlabroot
\toolbox\dotnetbuilder\bin\win64\<version>
\MWArray.dllMATLAB Runtime <MATLAB_RUNTIME_INSTALL_DIR>
\toolbox\dotnetbuilder\bin\win64\<version>
\MWArray.dllBuild and run the
PhoneBookComp
application in Visual Studio .NET.The application displays the following output:
Friends: 2x2 struct array with fields: name phone Result: 2x2 struct array with fields: name phone external Result record 2: Mary Smith 3912 (508) 555-3912 Entire structure: Number of Elements: 4 Dimensions: 2-by-2 Number of Fields: 3 Standard MATLAB view: 2x2 struct array with fields: name phone external Walking structure: Element 1 name: Jordan Robert phone: 3386 external: (508) 555-3386 Element 2 name: Mary Smith phone: 3912 external: (508) 555-3912 Element 3 name: Stacy Flora phone: 3238 external: (508) 555-3238 Element 4 name: Harry Alpert phone: 3077 external: (508) 555-3077
See Also
Library Compiler | compiler.build.dotNETAssembly
| deploytool