【转载】手把手教你用C#调用MATLAB函数
编译环境:Microsoft Visual Studio2008
版本9.0.21022.8RTM
https://www.doczj.com/doc/d819014049.html, Framework
版本 3.5
已安装的版本:Professional
Microsoft Visual Basic200891986-031-5000002-60050
Microsoft Visual Basic2008
Microsoft Visual C#200891986-031-5000002-60050
Microsoft Visual C#2008
Microsoft Visual C++200891986-031-5000002-60050
Microsoft Visual C++2008
Microsoft Visual Studio2008Tools for Office91986-031-5000002-60050
Microsoft Visual Studio2008Tools for Office
Microsoft Visual Web Developer200891986-031-5000002-60050
Microsoft Visual Web Developer2008
Crystal Reports AAJ6G-K0MSA4K-680002Z
Crystal Reports Basic for Visual Studio2008
Matlab2008b
首先需要做的工作是matlab编译器的设置,设置方法为:在Command window中输入mbuild-setup 显示如下
>>mbuild-setup
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers[y]/n?n%选择n
Select a compiler:
[1]Lcc-win32C2.4.1
[2]Microsoft Visual C++6.0
[3]Microsoft Visual C++.NET2003
[4]Microsoft Visual C++2005
[5]Microsoft Visual C++2005Express Edition
[6]Microsoft Visual C++2008
[0]None
Compiler:6%选择6,其他编译器可以选相应的选项,我没有验证过The default location for Microsoft Visual C++2008compilers is C:\Program Files\Microsoft Visual Studio9.0,
but that directory does not exist on this machine.
Use C:\Program Files\Microsoft Visual Studio9.0anyway[y]/n?n%选择n
Please enter the location of your compiler:[C:\Program Files\Microsoft Visual Studio9.0] D:\Program Files\Microsoft Visual Studio9.0%选择您自己的当前安装路径
Please verify your choices:
Compiler:Microsoft Visual C++2008
Location:D:\Program Files\Microsoft Visual Studio9.0
Are these correct[y]/n?y%看上述信息,如果正确选择y
**************************************************************************** Warning:Applications/components generated using Microsoft Visual Studio
2008require that the Microsoft Visual Studio2008run-time
libraries be available on the computer used for deployment.
To redistribute your applications/components,be sure that the
deployment machine has these run-time libraries.
**************************************************************************** Trying to update options file:C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R2008b\compopts.bat
From template: D:\PROGRA~1\MATLAB\R2008b\bin\win32\mbuildopts\msvc90compp.bat
Done... %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%至次编译器设置完成%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
在matlab的Command window中输入:
>>deploytool
Matlab产生deployment tool配置窗口
单击新建按钮,新建工程------>选择MATLAB Builder NE选项卡----->选择.NET Component 并添加工程名eaqual(测试用名,其实工程名称就是你导入C#后的命名空间名称,Matlab会在当前工作目录中新建文件夹文件夹名既为equal)---->点击ADD File按钮将你需要编译的MATLAB函数添加进当前工程,我这里导入的是D:\Program Files\MATLAB\R2008b\toolbox\aero\aero\angle2quat.m文件---->点击build.
deployment tool output窗口输出以下信息:
Build output(2009-03-2815:32:55).
[url=matlab:mcc%20-F%20%27E:%5CMATLAB%5Ceaqual.prj%27]mcc-F 'E:\MATLAB\eaqual.prj'[/url]
等待...................................有点久
当输出以下信息时编译成功了
Compiler version:4.9(R2008b)Warning:Duplicate directory name:E:\MATLAB.mcc-W 'dotnet:eaqual,Eaqual,0.0,private'-d'E:\MATLAB\eaqual\src'-T'link:lib'-v
'class{Eaqual:\Program Files\MATLAB\R2008b\toolbox\aero\aero\angle2quat.m}'
Warning:Name is nonexistent or not a directory:D:\Program Files\MATLAB\R2008b\toolbox\compiler\patch.Warning:Name is nonexistent or not a directory:D:\Program Files\MATLAB\R2008b\toolbox\compiler\patch.Processing D:\Program Files\MATLAB\R2008b\toolbox\aero\mcc.enc Processing D:\Program Files\MATLAB\R2008b\toolbox\matlab\mcc.enc Processing include files...2item(s)added. Processing directories installed with MCR...The file E:\MATLAB\eaqual\src\mccExcludedFiles.log contains a list of functions excluded from the CTF archive.2item(s)added.Generating MATLAB path for the compiled application... Created40path items.Begin validation of MEX files:Sat Mar2815:35:112009End validation of MEX files:Sat Mar2815:35:112009Warning:Name is nonexistent or not a directory:D:\Program Files\MATLAB\R2008b\toolbox\compiler\patch.Deleting0temporary MEX authorization files.Parsing file"D:\Program Files\MATLAB\R2008b\toolbox\aero\aero\angle2quat.m"(Referenced from:"Compiler Command Line").Parsing file"D:\Program Files\MATLAB\R2008b\toolbox\compiler\deploy\deployprint.m"(Referenced from:"Compiler Command Line").Parsing file"D:\Program Files\MATLAB\R2008b\toolbox\compiler\deploy\printdlg.m"(Referenced from:"Compiler Command Line").Generating file"E:\MATLAB\eaqual\src\Eaqual.cs".Generating file "E:\MATLAB\eaqual\src\EaqualNative.cs".Generating file "E:\MATLAB\eaqual\src\readme.txt".Generating file "E:\MATLAB\eaqual\src\dotnet_mcc_component_data.cs".Executing command: C:\WINDOWS\https://www.doczj.com/doc/d819014049.html,\Framework\v2.0.50727\csc.exe/nologo/optimize /doc:"E:\MATLAB\eaqual\src\eaqual.xml"/t:library/r:"D:\Program Files\MATLAB\R2008b\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll"
/resource:"E:\MATLAB\eaqual\src\eaqual.ctf"/out:"E:\MATLAB\eaqual\src\eaqual.dll" "E:\MATLAB\eaqual\src\Eaqual.cs"
"E:\MATLAB\eaqual\src\dotnet_mcc_component_data.cs"Executing command: C:\WINDOWS\https://www.doczj.com/doc/d819014049.html,\Framework\v2.0.50727\csc.exe/nologo/optimize /doc:"E:\MATLAB\eaqual\src\eaqualNative.xml"/t:library/r:"D:\Program Files\MATLAB\R2008b\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll"
/resource:"E:\MATLAB\eaqual\src\eaqual.ctf"
/out:"E:\MATLAB\eaqual\src\eaqualNative.dll""E:\MATLAB\eaqual\src\EaqualNative.cs" "E:\MATLAB\eaqual\src\dotnet_mcc_component_data.cs"
Copying:E:\MATLAB\eaqual\src\eaqual.dll->E:\MATLAB\eaqual\distrib
Copying:E:\MATLAB\eaqual\src\eaqualNative.dll->E:\MATLAB\eaqual\distrib Copying:E:\MATLAB\eaqual\src\readme.txt->E:\MATLAB\eaqual\distrib
Compilation completed succesfully.The output is located in E:\MATLAB\eaqual\distrib.
You can package the component by clicking on the"ackage"icon in the Deployment Tool
toolbar,or by clicking the Tools->ackage menu when the Deployment Tool panel is selected.To include additional files in the package,click Project->Settings).
生成的文件在工程文件夹里面
我的是E:\MATLAB\eaqual\distrib\angle2quat.dll
至此MATLAB的操作全部完成
打开VISUAL Studio2008新建C#工程我这里用的是控制台名为TestOfmatlab_consolt
我的是E:\Visual Studio2008\Projects\TEST\TestOfmatlab_consolt
添加引用E:\MATLAB\eaqual\distrib\angle2quat.dll
和D:\Program Files\MATLAB\R2008b\toolbox\dotnetbuilder\bin\win32\v2.0\MWArray.dll
(这里我没有用MATLAB画图,所以只引用MWArray.dll)
加入命名空间:
using https://www.doczj.com/doc/d819014049.html,.Arrays;
将下面代码加入Main函数
eaqual.Eaqual eq=new eaqual.Eaqual();
https://www.doczj.com/doc/d819014049.html,.Arrays.MWArray m=10,n=4,j=7,k;
MWArray S="XYZ";
k=eq.angle2quat((MWNumericArray)m,(MWNumericArray)n, (MWNumericArray)j,
(MWCharArray)S);
MWNumericArray temp=(MWNumericArray)k;
double[,]location=(double[,])temp.ToArray(MWArrayComponent.Real);
Console.WriteLine(location[0,0].ToString());
Console.WriteLine(location[0,1].ToString());
Console.WriteLine(k[2].ToString());
Console.WriteLine(k[3].ToString());
Console.WriteLine(k[4].ToString());
Console.ReadLine();
编译运行即可看到输出,此函数是姿态角转四元数函数,具体功能就不做说明了。这个例子输入输出参数的转换关系都有,大家自己看看就明白了