Integrate SimpleMATLABFunction Into .NET Application
Note
The examples for theMATLAB®Compiler SDK™product are in
, whereMatlabroot
\toolbox\dotnetbuilder\Examples\VS版本
vs
指定版本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功能位置 |
|
C#代码位置 |
|
视觉基本代码位置 |
|
程序
Copy the following folder that ships with the MATLAB product to your work folder:
Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\NET\PlotExample
在MATLAB命令提示下,导航到
PlotExample\PlotComp
subfolder in your work folder.Examine the
drawgraph
function located inPlotExample\PlotComp
.functiondrawgraph(coords) plot(coords(1,:), coords(2,:)); pause(5)
x = 0:0.01:10; y = sin(x); z = [x;y]; drawgraph(z)
The function outputs a figure that displays a sine wave.
Build the .NET component with theLibrary Compilerapp or
compiler.build.dotNETAssembly
using the following information:场地 价值 Library Name PlotComp
Class Name Plotter
File to Compile drawgraph.m
For example, if you are using
compiler.build.dotNETAssembly
, type:buildResults = compiler.build.dotnetassembly('drawgraph.m',...'AssemblyName','PlotComp',...'班级名称','Plotter');
For more details, see the instructions in生成.NET组件并构建.NET应用程序.
确定您是使用C#还是Visual Basic来访问组件。
C#
If you are using C#, write source code for a C# application that accesses the component.
此示例的示例申请在
PlotExample\PlotCSApp\PlotApp.cs
.This statement creates an instance of the
Plotter
班级:绘图器绘图器= new Ploter();
This statement explicitly casts the native
阴谋价值s
tomwnumericarray
and 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
.This statement creates an instance of the
Plotter
班级:DIM绘图仪为绘图器=新绘图仪
此语句调用方法
drawgraph
:阴谋ter.drawgraph(coords)
无论哪种情况,
PlotApp
程序执行以下操作:Creates two arrays of double values.
Creates a
Plotter
目的。Calls the
drawgraph
使用MATLAB绘制方程的方法阴谋
功能。用途
mwnumericarray
表示由drawgraph
method to plot the equation.使用
试着抓
block to catch and handle any exceptions.
使用Visual Studio打开与您的应用程序语言相对应的.NET项目文件。
C#
If you are using C#, the
plotcsapp
文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目plotcsapp.csproj
in视窗®资源管理器. You can also open it from the desktop by right-clickingplotcsapp.csprojand selectingOpen Outside MATLAB.视觉基本
If you are using Visual Basic, the
PlotVBApp
文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目PlotVBApp.vbproj
inWindows资源管理器. You can also open it from the desktop by right-clickingPlotVBApp.vbprojand selectingOpen Outside MATLAB.
Add a reference to your assembly file
PlotComp.dll
located in the folder where you generated or installed the assembly.Add a reference to the
MWArray
API.如果您的系统上安装了MATLAB Matlabroot
\toolbox\dotnetbuilder\bin\win64\
\MWArray.dllIfMATLAB Runtime已安装在您的系统上
\toolbox\dotnetbuilder\bin\win64\
\MWArray.dllBuild and run the
PlotApp
在Visual Studio .NET中应用。The application displays the following plot:
跟进此示例:
Try running the generated application on a different computer.
Try building an installer for the package using
compiler.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功能位置 |
|
C#代码位置 |
|
视觉基本代码位置 |
|
程序
Copy the following folder that ships with MATLAB to your work folder:
Matlabroot\toolbox\dotnetbuilder\Examples\VS版本\ net \ phonebookexample
在MATLAB命令提示下,导航到
PhoneBookexample \ PhoneBookComp
subfolder in your work folder.Examine the
makephone
function 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'}
Build the .NET component with theLibrary Compilerapp or
compiler.build.dotNETAssembly
using the following information:场地 价值 Library Name PhoneBookComp
Class Name PhoneBook
File to Compile makephone
For example, if you are using
compiler.build.dotNETAssembly
, type:buildResults = compiler.build.dotnetassembly('makephone.m',...'AssemblyName','PhoneBookComp',...'班级名称','PhoneBook');
For more details, see the instructions in生成.NET组件并构建.NET应用程序.
确定您是使用C#还是Visual Basic来访问组件。
C#
If you are using C#, write source code for a C# application that accesses the component.
此示例的示例申请在
PhoneBookExample\PhoneBookCSApp\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
程序执行以下操作:使用MWStructarray创建一个结构数组来表示包含名称和电话号码的示例电话簿数据。
实例化
Phonebook
班级为thePhonebook
object, as shown:thePhonebook = new phonebook();
Calls the MATLAB function
makephone
通过添加附加字段来创建结构的修改副本,如图所示:result = thePhonebook.makephone(1, friends);
显示生成的结构数组。
使用Visual Studio打开与您的应用程序语言相对应的.NET项目文件。
C#
If you are using C#, the
PhonebookCsapp
文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目Phonebookcsapp.csproj
inWindows资源管理器. You can also open it from the desktop by right-clickingPhonebookcsapp.csprojand selectingOpen Outside MATLAB.视觉基本
If you are using Visual Basic, the
PhoneBookVBApp
文件夹包含此示例的Visual Studio .NET项目文件。双击在Visual Studio .NET中打开项目phonebookvbapp.vbproj
inWindows资源管理器. You can also open it from the desktop by right-clickingphonebookvbapp.vbprojand selectingOpen Outside MATLAB.
Create a reference to your assembly file
PhoneBookComp.dll
located in the folder where you generated the assembly.Create a reference to the
MWArray
API, which is located in:MATLAB Matlabroot
\toolbox\dotnetbuilder\bin\win64\
\MWArray.dllMATLAB Runtime
\toolbox\dotnetbuilder\bin\win64\
\MWArray.dllBuild and run the
PhoneBookComp
在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
也可以看看
LibraryCompiler
|compiler.build.dotNETAssembly
|deploytool