This example is similar to Integrating a Simple MATLAB Function, except that the MATLAB® function to be encapsulated takes a variable number of arguments instead of just one.
The purpose of the example is to show you the following:
How to use the MATLAB
Compiler SDK™ product to convert
a MATLAB function,
drawgraph, which takes
a variable number of arguments, to a method of a .NET class (
and wrap the class in a .NET assembly (
drawgraph function (which can be called as
a method of the
Plotter class) displays a plot
of the input parameters.
How to access the component in a C# application (
by instantiating the
Plotter class and using
For information about these data conversion classes, see the MATLAB
MWArray Class Library Reference, available in the
matlabroot represents your MATLAB installation
How to build and run the
using the Visual Studio® .NET development environment.
If you have not already done so, copy the files for this example as follows:
Copy the following folder that ships with the MATLAB product to your work folder:
At the MATLAB command prompt,
VarArgExample subfolder in your work folder.
Write the MATLAB functions as you would any MATLAB function.
The code for the functions in this example is as follows:
This code is already in your work folder in
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:
|File to compile|
Write source code for an application that accesses the component.
The sample application for this example is in
The program listing is shown here.
The program does the following:
Initializes three arrays (
coords) using the
MWNumericArray to represent
the data needed by the methods
try-catch block to catch
and handle any exceptions
The following statements are alternative ways to call the
data= (MWNumericArray)plotter.drawgraph(colorSpec, coords, coords, coords,coords, coords); ... data= (MWNumericArray)plotter.drawgraph((MWArray)colorSpec, coords);
VarArgApp application using Visual Studio .NET.
VarArgCSApp folder contains a Visual Studio .NET
project file for this example. Open the project in Visual Studio .NET
VarArgCSApp.csproj in Windows® Explorer.
You can also open it from the desktop by right-clicking VarArgCSApp.csproj > Open Outside
Add a reference to the
Add or, if necessary, fix the location of a reference
VarArgComp component which you built in
a previous step. (The component,
is in the
of your work area.)
Build and run the application in Visual Studio .NET.