主要内容

将函数与变量数量的参数集成

这个例子展示了如何使用MATLAB创建一个。net应用程序®函数,接受可变数量的参数而不是一个。

在本例中,执行如下操作:

  • 使用MATLAB编译器SDK™乘积的MATLAB转换函数drawgraph.NET类的方法(绘图机),并将类包装在。net程序集中(VarArgComp).的drawgraph函数显示输入参数的绘图,并作为方法调用绘图机类。

  • 在c#应用程序中访问组件(VarArgApp.cs)或Visual Basic®应用程序(VarArgApp.vb)来实例化绘图机类和使用MWArray来表示数据。

  • 构建并运行VarArgDemoApp应用程序使用Visual Studio®net开发环境。

文件

MATLAB函数 drawgraph.m
extractcoords.m
MATLAB函数的位置 matlabroot\工具箱\ dotnetbuilder \ \和例子版本净\ VarArgExample \ VarArgComp \ \
c#代码的位置 matlabroot\工具箱\ dotnetbuilder \ \和例子版本\ \ VarArgExample \ VarArgCSApp净\ VarArgApp.cs
Visual Basic代码位置 matlabroot\工具箱\ dotnetbuilder \ \和例子版本\ \ VarArgExample \ VarArgVBApp净\ VarArgApp.vb

过程

  1. 复制以下文件夹,随MATLAB产品发货到您的工作文件夹:

    matlabroot\工具箱\ dotnetbuilder \ \和例子版本\ \ VarArgExample净

    在MATLAB命令提示符处,导航到新的VarArgExample \ VarArgComp子文件夹。

  2. 检查drawgraphextractcoords功能。

    函数[xyCoords] = drawtext (colorSpec, varargin) numVarArgIn= length(varargin);xyCoords = 0 (numVarArgIn, 2);x = 1:numVarArgIn xCoord = varargin{idx}(1);yCoord =变长度输入宗量{idx} (2);x (idx) = xCoord;y (idx) = yCoord;xyCoords (idx, 1) = xCoord;xyCoords (idx, 2) = yCoord;结束Xmin = min(0, min(x));Ymin = min(0, min(y));轴([xmin修复(max (x)) + 3 ymin修复(max (y)) + 3])情节(x, y,“颜色”, colorSpec);
    函数[varargout] = ExtractCoords(坐标)Idx = 1:nargout varargout{Idx}= coords(Idx,:);结束

  3. 控件来构建。net组件库的编译器应用程序或compiler.build.dotNETAssembly使用以下信息:

    价值
    库名称 VarArgComp
    类名 绘图机
    文件编制 extractcoords.mdrawgraph.m

    例如,如果你正在使用compiler.build.dotNETAssembly类型:

    buildResults = compiler.build.dotNETAssembly ([“extractcoords.m”“drawgraph.m”],...“AssemblyName”“VarArgComp”...“名称”“绘图机”);

    有关详细信息,请参阅生成。net程序集和构建。net应用程序

  4. 决定你是使用c#还是Visual Basic来访问组件。

    • c#

      如果您正在使用c#,请为访问该组件的c#应用程序编写源代码。

      此示例的示例应用程序位于VarArgExample \ VarArgCSApp \ VarArgApp.cs

      VarArgApp.cs

      下面的语句是调用的可选方法drawgraph方法:

      data = (MWNumericArray)绘图机。drawgraph(colorSpec, coords[0], coords[1], coords[2],coords[3], coords[4]); ... data= (MWNumericArray)plotter.drawgraph((MWArray)colorSpec, coords);
    • Visual Basic

      如果您正在使用Visual Basic,请为访问组件的Visual Basic应用程序编写源代码。

      此示例的示例应用程序位于VarArgExample \ VarArgVBApp \ VarArgApp.vb

      VarArgApp.vb

      下面的语句是调用的可选方法drawgraph方法:

      data = CType(绘图仪。drawgraph(colorSpec, coords(0), coords(1), coords(2), coords(3), coords(4)), MWNumericArray) ... data = CType(plotter.drawgraph(colorSpec, coords), MWNumericArray)

    在这两种情况下VarArgApp程序做以下工作:

    • 初始化三个数组(colorSpec数据,坐标)使用MWArray类库

    • 创建一个绘图机对象

    • 调用extracoordsdrawgraph方法

    • 使用MWNumericArray表示方法所需的数据

    • 使用一个try - catch块来捕获和处理任何异常

  5. 使用Visual Studio打开与应用程序语言对应的。net项目文件。

    • c#

      如果您正在使用c#,则VarArgCSApp文件夹中包含本例的Visual Studio . net项目文件。在Visual Studio .NET中双击打开项目VarArgCSApp.csproj窗户®资源管理器.你也可以从桌面上右键打开它VarArgCSApp.csproj并选择外开放的MATLAB

    • Visual Basic

      如果您正在使用Visual Basic,则VarArgVBApp文件夹中包含本例的Visual Studio . net项目文件。在Visual Studio .NET中双击打开项目VarArgVBApp.vbprojWindows资源管理器.你也可以从桌面上右键打开它VarArgVBApp.vbproj并选择外开放的MATLAB

  6. 创建对程序集文件的引用VarArgComp.dll位于生成或安装程序集的文件夹中。

  7. 的引用MWArrayAPI。

    如果您的系统上安装了MATLAB matlabroot\ \ dotnetbuilder \ bin \ win64 \工具箱< framework_version >\ MWArray.dll
    如果MATLAB运行时安装在您的系统上 < MATLAB_RUNTIME_INSTALL_DIR >\ \ dotnetbuilder \ bin \ win64 \工具箱< framework_version >\ MWArray.dll

  8. 构建并运行VarArgAppVisual Studio . net中的应用程序。

    程序显示如下输出:

    结果= 1 2 2 4 3 6 4 8 5 10
    一个图形,显示由坐标{1,2}、{2,4}、{3,6}、{4,8}和{5,10}创建的直线。

另请参阅

||

相关的话题