问题提出,如何把Matlab(本文简称MT)的算法集成到Visual Studio(本文简称VS)里面运行?
本文,通过编制一个MT中最简单的加法函数,我们把他做成 MSVS C#能够使用的动态库,说明了MSVS C# 和 MT集成的最小,然而,最简洁清晰的处理方法和过程。
1 VS2019
2 C# .Net 应用环境
3 MT2019
function result = addnum(a,b)
result = a+b;
end
保存为add.
我们改成:doadd
注意,Setting 里,路径最好改一下,否则,都到MT的路径里面去了:
路径笔者配置如下:
配置整体如下:
点击后,
会在设定目录下,生成动态库移植的所有信息,同时,还会给出,参考的测试源码,其实就是调用方法,还有生成动态库部署所需要的种种方面。
现在,我们研究一下,这些生成的文件情况,
先看看,在输出文件夹的三个定义,
我们先看看,readme.txt
add MATLAB .NET Assembly (.NET Component)
1. Prerequisites for Deployment 【部署要求】
Verify that version 9.7 (R2019b) of the MATLAB Runtime is installed.
If not, you can run the MATLAB Runtime installer.
To find its location, enter
>>mcrinstaller
at the MATLAB prompt.
NOTE: You will need administrator rights to run the MATLAB Runtime installer.Alternatively, download and install the Windows version of the MATLAB Runtime for R2019b
from the following link on the MathWorks website:https://www.mathworks.com/products/compiler/mcr/index.html
For more information about the MATLAB Runtime and the MATLAB Runtime installer, see
"Distribute Applications" in the MATLAB Compiler SDK documentation
in the MathWorks Documentation Center.2. Files to Deploy and Package【部署,需要打包的文件】
-add.dll
-contains the generated component using MWArray API.
-addNative.dll
-contains the generated component using native API.
-This readme file
Note: if end users are unable to download the MATLAB Runtime using the
instructions in the previous section, include it when building your
component by clicking the "Runtime included in package" link in the
Deployment Tool.
Auto-generated Documentation Templates:【自动生成的文档模板】MWArray.xml - This file contains the code comments for the MWArray data conversion
classes and their methods. This file can be found in either the component
distrib directory or in
*\toolbox\dotnetbuilder\bin\win64\v4.0 add_overview.html - HTML overview documentation file for the generated component. It
contains the requirements for accessing the component and for
generating arguments using the MWArray class hierarchy.add.xml - This file contains the code comments for the add component classes and methods.
Using a third party documentation tool, this file can be combined
with either or both of the previous files to generate online
documentation for the add component.
3. Resources【需要的资源】【案,MWArray 在做加法运算的时候使用了,这里从哪里获取MWArray 的正确版本,也以后集成 其他库必要考虑的问题】
To learn more about: See:
===================================================================
MWArray classes*\help\toolbox\
dotnetbuilder\MWArrayAPI\
MWArrayAPI.chm
Examples of .NET Web Applications Web Deployment in the MATLAB
.NET Assembly documentation in the
MathWorks Documentation Center
4. DefinitionsFor information on deployment terminology, go to
https://www.mathworks.com/help and select MATLAB Compiler >
Getting Started > About Application Deployment >
Deployment Product Terms in the MathWorks Documentation
Center.* NOTE:
is the directory where the MATLAB Runtime is installed on the target
machine.
is the directory where MATLAB is installed on the target machine.
里面就是真正需要加到MSVC C# 中去的动态库。
装完以下文件
【案,】框架的兼容性和MT一定要一致,否则,必出错!
MT2019,在生成动态库的时候,下方有 .net 框架的说明,如下:
点击后,会有如下的一段GUIDE说明,
错误信息:
末经处理的异常
始但攻正坝51友并吊。
第1个内部异常(共3 个》TypelnitializationException:“MathWorks.MATLAB.NET.Utility.MWMCR”的类型初始值设定项引发异常。
MWArray.dll,
笔者之前用的是MT runtime的库,这样运行的时候总报错,然后,改成MT安装时候的安装目录下的库,就可以了。
这个在比较复杂的MT库里面,经常出现。
项目引入新的库,但是,项目编译前没有执行清扫的任务:
出错信息:
TypelnitializationException:“MathWorks.MATLAB.NET.Arravs.MWArray”的类型初始值设定项引发异常。
C#中调用MATLAB执行MATLAB代码_c#连接matlab connecting to matlab unknown_matlab-CSDN博客
本例项目工程源码已经上传,可在我的资源下载。