詳解C#調(diào)用matlab生成的dll庫
matlab打包dll
1、matlab示例程序:
function untitled4(x)
z = peaks(x);
figure
surf(z)
end2、輸入deploytool打包matlab程序,具體如下:


3、拷貝
打包成功后,將生成for_redistribution_files_only文件夾中的dll文件拷貝到C#程序lib文件夾下,若沒有,新創(chuàng)建一個。


錯誤解決:

解決方法:將matlab程序改寫成一個方法。
C#調(diào)用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程序返回一個數(shù),而C#代碼接收的是MWArray數(shù)據(jù)。
p3.untitled4((MWArray)25);

運行結(jié)果:

到此這篇關(guān)于C#調(diào)用matlab生成的dll庫的文章就介紹到這了,更多相關(guān)C#調(diào)用matlab生成的dll庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#數(shù)值轉(zhuǎn)換-隱式數(shù)值轉(zhuǎn)換表參考
隱式轉(zhuǎn)換就是直接使用,比如可以把一個 byte 類型直接用在 int 上2013-04-04
C#跨平臺開發(fā)之使用C/C++生成的動態(tài)鏈接庫
這篇文章介紹了C#跨平臺開發(fā)之使用C/C++生成的動態(tài)鏈接庫,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-01-01
macOS系統(tǒng)下Vscode的python配置教程
這篇文章主要介紹了macOS系統(tǒng)下Vscode的python配置教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

