The purpose of these examples is to highlight main steps required for integrating a MATLAB® function.
To create the component for this example, see Integrating a Simple MATLAB Function. Then create a Visual Basic® application as follows:
Review the sample application for this example in
. matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NET\PlotExample\PlotVBApp\PlotApp.vb
The program listing is shown here.
The program does the following:
Creates two arrays of double values
Creates a Plotter
object
Calls the drawgraph
method to plot
the equation using the MATLAB plot
function
Uses MWNumericArray
to handle the
data needed by the drawgraph
method to plot the
equation
Note
For information about these data conversion classes, see the MATLAB
MWArray Class Library Reference, available in the
folder,
where matlabroot
\help\dotnetbuilder\MWArrayAPImatlabroot
represents your MATLAB installation
folder
Uses a try-catch
block to catch
and handle any exceptions
The statement
Dim plotter As Plotter = New Plotter
creates an instance of the Plotter
class,
and the statement
plotter.drawgraph(coords)
calls the method drawgraph
.
Build the PlotApp
application
using Visual Studio® .NET.
The PlotVBApp
folder contains a Visual Studio .NET
project file for this example. Open the project in Visual Studio .NET
by double-clicking PlotVBApp.vbproj
in Windows® Explorer.
You can also open it from the desktop by right-clicking PlotVBApp.vbproj > Open Outside
MATLAB.
Add a reference to the MWArray
component,
which is matlabroot
\toolbox\dotnetbuilder\bin\architecture
\framework_version
\mwarray.dll.
If necessary, add (or fix the location of) a reference
to the PlotComp
component which you built in a
previous step. (The component, PlotComp.dll
, is
in the \PlotExample\PlotComp\x86\V2.0\Debug\distrib
subfolder
of your work area.)
Build and run the application in Visual Studio .NET.
The makephone
function takes a structure
array as an input, modifies it, and supplies the modified array as
an output.
Note
For complete reference information about the MWArray
class
hierarchy, see the MWArray API documentation.
If you have not already done so, copy the files for this example as follows:
Copy the following folder that ships with MATLAB to your work folder:
matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\PhoneBookExample
At the MATLAB command prompt, cd
to
the new PhoneBookExample
subfolder in your work
folder.
Write the makephone
function as you
would any MATLAB function.
The following code defines the makephone
function:
function book = makephone(friends) book = friends; for i = 1:numel(friends) numberStr = num2str(book(i).phone); book(i).external = ['(508) 555-' numberStr]; end
This code is already in your work folder in PhoneBookExample\PhoneBookComp\makephone.m
.
From the MATLAB apps gallery, open the Library Compiler app.
Build the .NET component. See the instructions in Generate a .NET Assembly and Build a .NET Application for more details. Use the following information:
Project Name | PhoneBookComp |
Class Name | phonebook |
File to compile | makephone.m |
Write source code for an application that accesses the component.
The sample application for this example is in
.matlabroot
\toolbox\dotnetbuilder\Examples\VSVersion
\NETPhoneBookExample\PhoneBookVBApp\PhoneBookApp.vb
The program defines a structure array containing names and phone numbers, modifies it using a MATLAB function, and displays the resulting structure array.
The program listing is shown here.
The program does the following:
Creates a structure array, using MWStructArray
to
represent the example phonebook data.
Instantiates the plotter class as thePhonebook
object,
as shown: thePhonebook = new phonebook();
Calls the makephone
method to create
a modified copy of the structure by adding an additional field, as
shown: result = thePhonebook.makephone(1, friends);
Build thePhoneBookVBApp
application
using Visual Studio .NET.
The PhoneBookVBApp
folder contains
a Visual Studio .NET project file for this example. Open the project
in Visual Studio .NET by double-clicking PhoneBookVBApp.vbproj
in Windows Explorer.
You can also open it from the desktop by right-clicking PhoneBookVBApp.vbproj > Open
Outside MATLAB.
Add a reference to the MWArray
component,
which is matlabroot
\toolbox\dotnetbuilder\bin\architecture
\framework_version
\mwarray.dll.
If necessary, add (or fix the location of) a reference
to the PhoneBookVBComp
component which you built
in a previous step. (The component, PhoneBookComp.dll
,
is in the \PhoneBookExample\PhoneBookVBApp\x86\V2.0\Debug\distrib
subfolder
of your work area.)
Build and run the application in Visual Studio .NET.
The getphone
program should display the 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