【C#-1】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);

运行结果:

相关推荐
唐青枫3 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20109 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间3 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#