MSVS C# Matlab的混合编程系列1 - 看似简单的问题引出

7 篇文章 0 订阅
本文详细描述了如何将MATLAB的算法集成到VisualStudioC#环境中,通过创建一个简单的加法函数动态库,探讨了版本一致性、部署工具的使用以及常见问题,如框架兼容性和库文件冲突等。
摘要由CSDN通过智能技术生成

前言:

问题提出,如何把Matlab(本文简称MT)的算法集成到Visual Studio(本文简称VS)里面运行?

本文,通过编制一个MT中最简单的加法函数,我们把他做成 MSVS C#能够使用的动态库,说明了MSVS C# 和 MT集成的最小,然而,最简洁清晰的处理方法和过程。 


环境:

1 VS2019

2 C# .Net 应用环境

3 MT2019


写在前面的结论:

MT的库无论是安装版本还是Runtime,1 一定要用一致的版本 2一定要充分利用生成的移植文件,

一致版本,

  • 指的是MT的.net的框架和VSC3的.net框架版本一致,

  • 也包括MT的通用库版本比如,2019的库,和2022的库有可能不一致

  • 硬件平台设定的一致性

  • 利用MT的deploytool工具:

    • 生成动态库:
  • 在MSVS中,引入这个动态库:

  • 调用这个动态库的方法

  • 说明过程中遇到的三个容易忽视的问题:


步骤实录:

1 MATLAB中,设计一个简单的加法函数:

function result = addnum(a,b)
    result = a+b;
end

保存为add.

2 调用deploytool的库编译工具:

3 用Library Compiler进行编译:

3.1 VS C# 中选:

 3.2 可以改一名字:

 我们改成:doadd

 注意,Setting 里,路径最好改一下,否则,都到MT的路径里面去了:

路径笔者配置如下:

配置整体如下:

点击后,

会在设定目录下,生成动态库移植的所有信息,同时,还会给出,参考的测试源码,其实就是调用方法,还有生成动态库部署所需要的种种方面。

一定要充分利用生成的移植文件,

现在,我们研究一下,这些生成的文件情况,

 3.3  不同路径下生成文件对比:

先看看,在输出文件夹的三个定义,


3.3.1 Testing files 文件夹(测试文件)

 

 我们先看看,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
              <mcr_root>*\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                    <matlab_root>*\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. Definitions

For 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: <mcr_root> is the directory where the MATLAB Runtime is installed on the target 
        machine.
        <matlab_root> is the directory where MATLAB is installed on the target machine.

 3.3.2 EndUser 文件夹(终端用户文件)

 里面就是真正需要加到MSVC C# 中去的动态库。

 

 

装完以下文件 


4 构建一个C#的项目,调用我们的MT的动态库:

4.1 构建一个窗体项目:

 【案,】框架的兼容性和MT一定要一致,否则,必出错!

4.2 设定一个Button,来触发MT动态库的调用

4.3 确保你的目标框架和MT是兼容的:

4.4 引入编好的动态库:

 

4.5 配置好你 编译平台,X64大多数情况下:

5  插入调用dll库的代码:       

using MathWorks.MATLAB.NET.Arrays;
using add; // 这里改成你的库名,我们刚才编译的加法库
using MathWorks.MATLAB.NET.Utility;

6 运行结果展示:

 


 可能遇到的问题:

1 兼容性问题:

1.1 框架兼容:

MT2019,在生成动态库的时候,下方有 .net 框架的说明,如下:

点击后,会有如下的一段GUIDE说明,

 错误信息:

末经处理的异常
始但攻正坝51友并吊。
第1个内部异常(共3 个》TypelnitializationException:“MathWorks.MATLAB.NET.Utility.MWMCR”的类型初始值设定项引发异常。

 

1.2 MT 的通用动态库的兼容性:例如:MWArray.dll

MWArray.dll,

笔者之前用的是MT runtime的库,这样运行的时候总报错,然后,改成MT安装时候的安装目录下的库,就可以了。

2 项目引入的库不兼容:

这个在比较复杂的MT库里面,经常出现。

3 编译前没有执行清扫:

项目引入新的库,但是,项目编译前没有执行清扫的任务:

出错信息:

TypelnitializationException:“MathWorks.MATLAB.NET.Arravs.MWArray”的类型初始值设定项引发异常。


参考: 

 C#中调用MATLAB执行MATLAB代码_c#连接matlab connecting to matlab unknown_matlab-CSDN博客


源码:

本例项目工程源码已经上传,可在我的资源下载。 

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Franklin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值