主要内容

本页采用了机器翻译。点击此处可查看英文原文。

具有可变数量参量的积分函数

R2026b

此示例向您说明如何使用采用可变数量参量(而不是只采用一个参量)的 MATLAB® 函数创建 .NET 应用程序。

在此示例中,您执行以下步骤:

  • 使用 MATLAB Compiler SDK™ 产品将 MATLAB 函数 drawgraph 转换为 .NET 类 (Plotter) 的方法,并将该类包装在 .NET 程序集 (VarArgComp) 中。drawgraph 函数显示输入参数的图,并作为 Plotter 类的方法调用。

  • 通过实例化 VarArgApp.cs 类并使用 VarArgApp.vb 来表示数据,在 C# 应用程序 (Plotter) 或 Visual Basic® 应用程序 (MWArray) 中访问该组件。

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

文件

MATLAB 函数drawgraph.m
extractcoords.m
MATLAB 函数位置matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\VarArgExample\VarArgComp\
C# 代码位置matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\VarArgExample\VarArgCSApp\VarArgApp.cs
Visual Basic 代码位置matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\VarArgExample\VarArgVBApp\VarArgApp.vb

过程

  1. 将 MATLAB 产品附带的以下文件夹复制到您的工作文件夹:

    matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\VarArgExample
    

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

  2. 检查 drawgraph 和 extractcoords 函数。

    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
    

  3. 使用以下信息,使用 .NET 程序集编译器或 compiler.build.dotNETAssembly 构建 .NET 组件:

    字段值
    库名称VarArgComp
    类名Plotter
    要编译的文件extractcoords.m drawgraph.m

    例如,如果您使用的是 compiler.build.dotNETAssembly,请输入:

    buildResults = compiler.build.dotNETAssembly(["extractcoords.m","drawgraph.m"], ...
    'AssemblyName','VarArgComp', ...
    'ClassName','Plotter');

    有关详细信息,请参阅生成 MWArray .NET 程序集并构建 .NET 应用程序中的说明。

  4. 确定是否使用 C# 还是 Visual Basic 来访问组件。

    • C#

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

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

      using System;
      
      using MathWorks.MATLAB.NET.Utility;
      using MathWorks.MATLAB.NET.Arrays;
      
      using VarArgComp;
      
      
      namespace MathWorks.Examples.VarArgApp
       {
        /// <summary>
        /// This application demonstrates how to call components 
        ///  having methods with varargin/vargout arguments.
        /// </summary>
        class VarArgApp
          {
            #region MAIN
      
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
              {
                // Initialize the input data
                MWNumericArray colorSpec= new double[]
                                              {0.9, 0.0, 0.0};
                MWNumericArray data= 
                        new MWNumericArray(new int[,]{{1,2},{2,4},
                                             {3,6},{4,8},{5,10}});  
                MWArray[] coords= null;
      
                try
                  {
                    // Create a new plotter object
                    Plotter plotter= new Plotter();
      
                    //Extract a variable number of two element x and y coordinate 
                    //  vectors from the data array
                    coords= plotter.extractcoords(5, data);
      
                    // Draw a graph using the specified color to connect the 
                    //   variable number of input coordinates.
                    // Return a two column data array containing the input coordinates.
                    data= (MWNumericArray)plotter.drawgraph(colorSpec, 
                      coords[0], coords[1], coords[2],coords[3], coords[4]);
      
                    Console.WriteLine("result=\n{0}", data);
      
                    Console.ReadLine();  // Wait for user to exit application
      
                    // Note: You can also pass in the coordinate array directly.
                    data= (MWNumericArray)plotter.drawgraph(colorSpec, coords);
      
                    Console.WriteLine("result=\n{0}", data); 
       
                    Console.ReadLine();  // Wait for user to exit application
                  }
      
                catch(Exception exception)
                  {
                    Console.WriteLine("Error: {0}", exception);
                  }
              }
      
            #endregion
          }
        }
      

      以下语句是调用 drawgraph 方法的替代方法:

      data= (MWNumericArray)plotter.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 中。

      Imports System
      
      Imports MathWorks.MATLAB.NET.Utility
      Imports MathWorks.MATLAB.NET.Arrays
      
      Imports VarArgComp
      
      
      Namespace MathWorks.Demo.VarArgDemoApp
      
      
      ' <summary>
      ' This application demonstrates how to call components having methods with 
      '   varargin/vargout arguments.
      ' </summary>
      Class VarArgDemoApp
          
      #Region " MAIN "
      
              ' <summary>
              ' The main entry point for the application.
              ' </summary>
              Shared Sub Main(ByVal args() As String)
      
                  ' Initialize the input data
                  Dim colorSpec As MWNumericArray = 
                    New MWNumericArray(New Double() {0.9, 0.0, 0.0})
                  Dim data As MWNumericArray = 
                    New MWNumericArray(New Integer(,) {{1, 2}, {2, 4}, {3, 6}, {4, 8}, {5, 10}})
                  Dim coords() As MWArray = Nothing
      
                  Try
      
                      ' Create a new plotter object
                      Dim plotter As Plotter = New Plotter
      
                      'Extract a variable number of two element x and y coordinate 
                      ' vectors from the data array
                      coords = plotter.extractcoords(5, data)
      
                      ' Draw a graph using the specified color to connect the variable number of 
                      '  input coordinates.
                      ' Return a two column data array containing the input coordinates.
                      data = CType(plotter.drawgraph(colorSpec, coords(0), coords(1), coords(2), 
                         coords(3), coords(4)), _
                                   MWNumericArray)
      
                      Console.WriteLine("result={0}{1}", Chr(10), data)
      
                      Console.ReadLine()  ' Wait for user to exit application
      
                      ' Note: You can also pass in the coordinate array directly.
                      data = CType(plotter.drawgraph(colorSpec, coords), MWNumericArray)
      
                      Console.WriteLine("result=\{0}{1}", Chr(10), data)
      
                      Console.ReadLine()  ' Wait for user to exit application
      
      
                  Catch exception As Exception
                      Console.WriteLine("Error: {0}", exception)
                  End Try
              End Sub
      #End Region
      
          End Class
      End Namespace
      

      以下语句是调用 drawgraph 方法的替代方法:

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

    无论哪种情况,VarArgApp 程序都会执行以下操作:

    • 使用 colorSpec 类库初始化三个数组(data、coords 和 MWArray)

    • 创建 Plotter 对象

    • 调用 extracoords 和 drawgraph 方法

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

    • 使用 try-catch 模块捕获并处理任何异常

  5. 使用 Visual Studio 打开与您的应用程序语言相对应的 .NET 工程文件。

    • C#

      如果您使用的是 C#,则 VarArgCSApp 文件夹包含此示例的 Visual Studio .NET 工程文件。在 Windows® 资源管理器中双击 VarArgCSApp.csproj 在 Visual Studio .NET 中打开该工程。您也可以通过右键点击 VarArgCSApp.csproj 并选择在 MATLAB 外部打开从桌面打开它。

    • Visual Basic

      如果您使用的是 Visual Basic,则 VarArgVBApp 文件夹包含此示例的 Visual Studio .NET 工程文件。在 Windows 资源管理器中双击 VarArgVBApp.vbproj 在 Visual Studio .NET 中打开该工程。您也可以通过右键点击 VarArgVBApp.vbproj 并选择在 MATLAB 外部打开从桌面打开它。

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

  7. 创建对 MWArray API 的引用。

    如果您的系统上安装了 MATLABmatlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll
    如果您的系统上安装了 MATLAB Runtime<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll

  8. 在 Visual Studio .NET 中构建并运行 VarArgApp 应用程序。

    该程序显示以下输出:

    result=
         1     2
         2     4
         3     6
         4     8
         5    10
    
    A figure that displays the line created by the coordinates {1, 2}, {2, 4}, {3, 6}, {4, 8}, and {5, 10}.

另请参阅

主题