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);

运行结果:
