Main Content

Integrate SimpleMATLABFunction Into .NET Application

Note

The examples for theMATLAB®Compiler SDK™product are inMatlabroot\toolbox\dotnetbuilder\Examples\VS版本, wherevs版本指定版本Microsoft®视觉工作室®.NET您正在使用。您可以通过在Visual Studio中打开以下解决方案来为所有示例加载项目:

Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ dotnetexamples.sln

The简单的情节example shows you how to create a .NET assembly that calls a MATLAB function to display a plot. For an example that uses a MATLAB function to modify a structure array, seePhone Book.

在以下示例中,您执行以下步骤将MATLAB函数集成到.NET应用程序中:

  • Use theMATLAB编译器SDKproduct to convert a MATLAB function to a method of a .NET class and wrap the class in a .NET assembly.

  • 访问C#应用程序或Visual Basic中的组件®通过实例化.NET类并使用MWArray班级库处理数据转换。

  • Build and run the generated application using the Visual Studio .NET development environment.

Prerequisites

  • Verify that you have met all of theMATLAB编译器SDK.NET target requirements. For details, seeMATLAB编译器SDK .NET目标要求.

  • Verify that you haveMicrosoft Visual Studioinstalled.

  • End users must have an installation ofMATLAB Runtime运行应用程序。有关详细信息,请参阅Install and Configure MATLAB Runtime.

    For testing purposes, you can use an installation of MATLAB instead ofMATLAB Runtime.

创建简单的图

Files

MATLAB功能位置 Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\NET\PlotExample\PlotComp\drawgraph.m
C#代码位置 Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ net \ plotexample \ plotcsapp \ plotapp.cs
视觉基本代码位置 Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\NET\PlotExample\PlotVBApp\PlotApp.vb

程序

  1. Copy the following folder that ships with the MATLAB product to your work folder:

    Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\NET\PlotExample

    在MATLAB命令提示下,导航到PlotExample\PlotCompsubfolder in your work folder.

  2. Examine thedrawgraphfunction located inPlotExample\PlotComp.

    functiondrawgraph(coords) plot(coords(1,:), coords(2,:)); pause(5)
    测试函数在MATLAB命令提示符。

    x = 0:0.01:10; y = sin(x); z = [x;y]; drawgraph(z)

    The function outputs a figure that displays a sine wave.

  3. Build the .NET component with theLibrary Compilerapp orcompiler.build.dotNETAssemblyusing the following information:

    场地 价值
    Library Name PlotComp
    Class Name Plotter
    File to Compile drawgraph.m

    For example, if you are usingcompiler.build.dotNETAssembly, type:

    buildResults = compiler.build.dotnetassembly('drawgraph.m',...'AssemblyName','PlotComp',...'班级名称','Plotter');

    For more details, see the instructions in生成.NET组件并构建.NET应用程序.

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

    • C#

      If you are using C#, write source code for a C# application that accesses the component.

      此示例的示例申请在PlotExample\PlotCSApp\PlotApp.cs.

      PlotApp.cs

      This statement creates an instance of thePlotter班级:

      绘图器绘图器= new Ploter();

      This statement explicitly casts the native阴谋价值stomwnumericarrayand then calls the methoddrawgraph:

      plotter.drawgraph(((mwnumericArray)plotValues);
    • 视觉基本

      If you are using Visual Basic, write source code for a Visual Basic application that accesses the component.

      此示例的示例申请在PlotExample\PlotVBApp\PlotApp.vb.

      plotapp.vb

      This statement creates an instance of thePlotter班级:

      DIM绘图仪为绘图器=新绘图仪

      此语句调用方法drawgraph:

      阴谋ter.drawgraph(coords)

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

    • Creates two arrays of double values.

    • Creates aPlotter目的。

    • Calls thedrawgraph使用MATLAB绘制方程的方法阴谋功能。

    • 用途mwnumericarray表示由drawgraphmethod to plot the equation.

    • 使用试着抓block to catch and handle any exceptions.

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

    • C#

      If you are using C#, theplotcsapp文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目plotcsapp.csprojin视窗®资源管理器. You can also open it from the desktop by right-clickingplotcsapp.csprojand selectingOpen Outside MATLAB.

    • 视觉基本

      If you are using Visual Basic, thePlotVBApp文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目PlotVBApp.vbprojinWindows资源管理器. You can also open it from the desktop by right-clickingPlotVBApp.vbprojand selectingOpen Outside MATLAB.

  6. Add a reference to your assembly filePlotComp.dlllocated in the folder where you generated or installed the assembly.

  7. Add a reference to theMWArrayAPI.

    如果您的系统上安装了MATLAB Matlabroot\toolbox\dotnetbuilder\bin\win64\\MWArray.dll
    IfMATLAB Runtime已安装在您的系统上 \toolbox\dotnetbuilder\bin\win64\\MWArray.dll

  8. Build and run thePlotApp在Visual Studio .NET中应用。

    The application displays the following plot:

    Plot of x squared.

  9. 跟进此示例:

    • Try running the generated application on a different computer.

    • Try building an installer for the package usingcompiler.package.installer.

    • 尝试集成由多个功能组成的组件。

Create Phone Book

In this example, you create a .NET assembly that calls a MATLAB function to modify a structure array that contains phone numbers.

Files

MATLAB功能位置 Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ net \ phonebookexample\PhoneBookComp\makephone.m
C#代码位置 Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ net \ phonebookexample \ phonebookcsapp \ phonebookapp.cs
视觉基本代码位置 Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ net \ phonebookexample \ phonebookvbapp \ phonebookapp.vb

程序

  1. Copy the following folder that ships with MATLAB to your work folder:

    Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ net \ phonebookexample

    在MATLAB命令提示下,导航到PhoneBookexample \ PhoneBookCompsubfolder in your work folder.

  2. Examine themakephonefunction located inPhoneBookexample \ PhoneBookComp.

    functionbook = makephone(friends) book = friends;为了i = 1:numel(friends) numberStr = num2str(book(i).phone); book(i).external = ['(508)555-'numberStr];end

    测试函数在MATLAB命令提示符。

    friends(1).name =“乔丹·罗伯特”;朋友(1).phone = 3386;朋友(2).name =“玛丽·史密斯”;friends(2).phone = 3912; struct2table(makephone(friends))
    ans = 2×3 table name phone external _______________ _____ __________________ "Jordan Robert" 3386 {'(508) 555-3386'} "Mary Smith" 3912 {'(508) 555-3912'}
  3. Build the .NET component with theLibrary Compilerapp orcompiler.build.dotNETAssemblyusing the following information:

    场地 价值
    Library Name PhoneBookComp
    Class Name PhoneBook
    File to Compile makephone

    For example, if you are usingcompiler.build.dotNETAssembly, type:

    buildResults = compiler.build.dotnetassembly('makephone.m',...'AssemblyName','PhoneBookComp',...'班级名称','PhoneBook');

    For more details, see the instructions in生成.NET组件并构建.NET应用程序.

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

    • C#

      If you are using C#, write source code for a C# application that accesses the component.

      此示例的示例申请在
      PhoneBookExample\PhoneBookCSApp\PhoneBookApp.cs.

      PhonebookApp.cs

    • 视觉基本

      If you are using Visual Basic, write source code for a Visual Basic application that accesses the component.

      此示例的示例申请在
      \ phonebookexample \ phonebookvbapp \ phonebookapp.vb.

      PhonebookApp.vb

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

    • 使用MWStructarray创建一个结构数组来表示包含名称和电话号码的示例电话簿数据。

    • 实例化Phonebook班级为thePhonebookobject, as shown:
      thePhonebook = new phonebook();

    • Calls the MATLAB functionmakephone通过添加附加字段来创建结构的修改副本,如图所示:
      result = thePhonebook.makephone(1, friends);

    • 显示生成的结构数组。

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

    • C#

      If you are using C#, thePhonebookCsapp文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目Phonebookcsapp.csprojinWindows资源管理器. You can also open it from the desktop by right-clickingPhonebookcsapp.csprojand selectingOpen Outside MATLAB.

    • 视觉基本

      If you are using Visual Basic, thePhoneBookVBApp文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目phonebookvbapp.vbprojinWindows资源管理器. You can also open it from the desktop by right-clickingphonebookvbapp.vbprojand selectingOpen Outside MATLAB.

  6. Create a reference to your assembly filePhoneBookComp.dlllocated in the folder where you generated the assembly.

  7. Create a reference to theMWArrayAPI, which is located in:

    MATLAB Matlabroot\toolbox\dotnetbuilder\bin\win64\\MWArray.dll
    MATLAB Runtime \toolbox\dotnetbuilder\bin\win64\\MWArray.dll

  8. Build and run thePhoneBookComp在Visual Studio .NET中应用。

    The application displays the following 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

也可以看看

||

相关话题