详解C#调用matlab生成的dll库
作者:WXG1011
这篇文章主要介绍了C#调用matlab生成的dll库,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
matlab打包dll
1、matlab示例程序:
function untitled4(x) z = peaks(x); figure surf(z) end
2、输入deploytool打包matlab程序,具体如下:
3、拷贝
打包成功后,将生成for_redistribution_files_only文件夹中的dll文件拷贝到C#程序lib文件夹下,若没有,新创建一个。
错误解决:
解决方法:将matlab程序改写成一个方法。
C#调用dll
1、添加引用
MWArray.dll在matlab安装目录..\matlab\toolbox\dotnetbuilder\bin\win64\v4.0下,untitled4.ll与untitled4Native.dll在C#工程lib文件夹下。
2、导入包
using untitled4; using MathWorks.MATLAB.NET.Arrays;
3、添加button点击事件
private void Button1_Click(object sender, EventArgs e) { untitled4.Class1 p3 = new untitled4.Class1(); p3.untitled4((MWArray)25); }
完整示例代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using untitled4; using MathWorks.MATLAB.NET.Arrays; namespace test1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void Button1_Click(object sender, EventArgs e) { untitled4.Class1 p3 = new untitled4.Class1(); p3.untitled4((MWArray)25); } } }
错误解决1:
解决方法:debug平台改为×64位
错误解决2:
解决方法:matlab程序返回一个数,而C#代码接收的是MWArray数据。
p3.untitled4((MWArray)25);
运行结果:
到此这篇关于C#调用matlab生成的dll库的文章就介绍到这了,更多相关C#调用matlab生成的dll库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!