当前位置: 首页 > news >正文

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

前言:

问题提出,如何把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博客


源码:

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Android学习(五):常用控件
  • 【EI会议征稿通知】2024年通信安全与信息处理国际学术会议(CSIP 2024)
  • C#调用C动态链接库
  • MicroPython核心(1):源码获取、编译构建
  • GO 中如何防止 goroutine 泄露
  • Spring Boot各类变量的使用
  • Redis原理篇(String)
  • MVC的设计理念
  • NLP自然语言处理介绍
  • openssl3.2 - linux脚本(.sh)调用openssl命令行参数的简单确认方法
  • 【迅搜19】扩展(二)TNTSearch和JiebaPHP方案
  • 5G_射频测试_参考规范(一)
  • ssh:connect to host github.com port 22: Connection timed out
  • pyspark笔记:over
  • Halcon 边缘提取
  • 【Linux系统编程】快速查找errno错误码信息
  • 2017 前端面试准备 - 收藏集 - 掘金
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Logstash 参考指南(目录)
  • storm drpc实例
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 从0到1:PostCSS 插件开发最佳实践
  • 力扣(LeetCode)22
  • 算法---两个栈实现一个队列
  • 为视图添加丝滑的水波纹
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 在Docker Swarm上部署Apache Storm:第1部分
  • Linux权限管理(week1_day5)--技术流ken
  • ​linux启动进程的方式
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • (2024,RWKV-5/6,RNN,矩阵值注意力状态,数据依赖线性插值,LoRA,多语言分词器)Eagle 和 Finch
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (ibm)Java 语言的 XPath API
  • (PADS学习)第二章:原理图绘制 第一部分
  • (rabbitmq的高级特性)消息可靠性
  • (九)信息融合方式简介
  • (四)事件系统
  • (转)Sublime Text3配置Lua运行环境
  • (自适应手机端)行业协会机构网站模板
  • . NET自动找可写目录
  • .net core 管理用户机密
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .NetCore项目nginx发布
  • .NET命令行(CLI)常用命令
  • @Autowired注解的实现原理
  • [ C++ ] 类和对象( 下 )
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [Android]Tool-Systrace
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配
  • [C#]OpenCvSharp使用帧差法或者三帧差法检测移动物体
  • [C#]使用DlibDotNet人脸检测人脸68特征点识别人脸5特征点识别人脸对齐人脸比对FaceMesh
  • [C/C++入门][字符与ASCII码]6、用代码来转换字符与它的ASCII码
  • [C++] 统计程序耗时