Integrate Function with Variable Number of Arguments
This example shows you how to create a .NET application using a MATLAB® function that takes a variable number of arguments instead of just one.
In this example, you perform the following steps:
Use the MATLAB Compiler SDK™ product to convert the MATLAB function
drawgraphto a method of a .NET class (Plotter) and wrap the class in a .NET assembly (VarArgComp). Thedrawgraphfunction displays a plot of the input parameters and is called as a method of thePlotterclass.Access the component in a C# application (
VarArgApp.cs) or a Visual Basic® application (VarArgApp.vb) by instantiating thePlotterclass and usingMWArrayto represent data.Build and run the
VarArgDemoAppapplication using the Visual Studio® .NET development environment.
Files
| MATLAB Functions | drawgraph.mextractcoords.m |
| 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\VarArgExample
At the MATLAB command prompt, navigate to the new
VarArgExample\VarArgCompsubfolder in your work folder.Examine the
drawgraphandextractcoordsfunctions.function [xyCoords] = DrawGraph(colorSpec, varargin) numVarArgIn= length(varargin); xyCoords= zeros(numVarArgIn, 2); for idx = 1:numVarArgIn xCoord = varargin{idx}(1); yCoord = varargin{idx}(2); x(idx) = xCoord; y(idx) = yCoord; xyCoords(idx,1) = xCoord; xyCoords(idx,2) = yCoord; end xmin = min(0, min(x)); ymin = min(0, min(y)); axis([xmin fix(max(x))+3 ymin fix(max(y))+3]) plot(x, y, 'color', colorSpec);
function [varargout] = ExtractCoords(coords) for idx = 1:nargout varargout{idx}= coords(idx,:); end
Build the .NET component with the .NET Assembly Compiler app or
compiler.build.dotNETAssemblyusing the following information:Field Value Library Name VarArgCompClass Name PlotterFiles to Compile extractcoords.mdrawgraph.mFor example, if you are using
compiler.build.dotNETAssembly, type:buildResults = compiler.build.dotNETAssembly(["extractcoords.m","drawgraph.m"], ... 'AssemblyName','VarArgComp', ... '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
VarArgExample\VarArgCSApp\VarArgApp.cs.The following statements are alternative ways to call the
drawgraphmethod:data= (MWNumericArray)plotter.drawgraph(colorSpec, coords[0], coords[1], coords[2],coords[3], coords[4]); ... data= (MWNumericArray)plotter.drawgraph((MWArray)colorSpec, coords);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
VarArgExample\VarArgVBApp\VarArgApp.vb.The following statements are alternative ways to call the
drawgraphmethod:data = CType(plotter.drawgraph(colorSpec, coords(0), coords(1), coords(2), coords(3), coords(4)), MWNumericArray) ... data = CType(plotter.drawgraph(colorSpec, coords), MWNumericArray)
In either case, the
VarArgAppprogram does the following:Initializes three arrays (
colorSpec,data, andcoords) using theMWArrayclass libraryCreates a
PlotterobjectCalls the
extracoordsanddrawgraphmethodsUses
MWNumericArrayto represent the data needed by the methodsUses a
try-catchblock 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
VarArgCSAppfolder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingVarArgCSApp.csprojin Windows® Explorer. You can also open it from the desktop by right-clicking VarArgCSApp.csproj and selecting Open Outside MATLAB.Visual Basic
If you are using Visual Basic, the
VarArgVBAppfolder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingVarArgVBApp.vbprojin Windows Explorer. You can also open it from the desktop by right-clicking VarArgVBApp.vbproj and selecting Open Outside MATLAB.
Create a reference to your assembly file
VarArgComp.dlllocated in the folder where you generated or installed the assembly.Create a reference to the
MWArrayAPI.If MATLAB is installed on your system matlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dllIf MATLAB Runtime is installed on your system <MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dllBuild and run the
VarArgAppapplication in Visual Studio .NET.The program displays the following output:
result= 1 2 2 4 3 6 4 8 5 10
