vs调用matlab生成的dll,VS2008中C#调用Matlab生成的DLL文件

打开Matlab 2009a,新建一个.m文件,输入如下代码:

function result=twice(inputvar);

result=2*inputvar;

将代码保存为twice.m文件,如图所示。

25550300_1.png

2、使用Matlab DeployTool生成.Net组件

在Matlab 2009a的Command Window中输入deploytool并回车,如图所示。

25550300_2.png

就可以打开Matlab的DeployTool,如图所示。

25550300_3.png

在DeployTool窗体中,点击新建按钮,打开新建项目对话框,如图所示。

25550300_4.png

在左边的列表中选择MATLAB Builder NE,并在右边的列表中选择.NET Component,我们将这个工程命名为twice.prj,并点击OK,则进入项目设置界面,如图所示。

25550300_5.png

在Twice文件夹中添加我们刚才建好的.m文件,如图所示。

25550300_6.png

点击编译按钮或使用快捷键Ctrl+B则对该工程进行编译。编译完成后的主窗口如图所示。

25550300_7.png

3、创建一个测试用的Windows应用程序,将其命名为MatlabTest,如图所示。

25550300_8.png

4、添加对MWArray.dll的引用

MWArray.dll一般位于[Matlab安装位置]\R2009a\toolbox\dotnetbuilder\bin\win32\v2.0文件夹中,我们注意到Matlab 2009a对应的MWArray.dll是2.9.0.0版本的。在进行Matlab版本升级的时候,比较头疼的就是这个dll。

25550300_9.png

因为版本升级意味着之前生成的dll都需要重新生成。关于版本升级的问题,我将在后面的博文中详细介绍。

之所以添加MWArray.dll,是因为在这种混合编程方式下,我们经常要

using MathWorks.MATLAB.NET.Arrays;

using MathWorks.MATLAB.NET.Utility;

5、添加对Matlab生成的twice.dll的引用。

6、添加窗体中相应的组件。

添加一个Label控件,并修改其Text属性为“计算结果”;

添加一个Label控件,用于显示计算结果;

添加一个Button控件,用于执行计算方法。

设计好后的Form1窗体如图所示。

25550300_10.png

7、编写相应的后台代码。

C#代码 icon_copy.gif icon_star.png

spinner.gif

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingMathWorks.MATLAB.NET.Arrays;

usingtwice;

namespaceMatlabTest

{

publicpartialclassForm1 : Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender, EventArgs e)

{

inti = 5;

Twice c =newTwice();

MWArray result = c.twice((MWArray)i);

this.label2.Text = result.ToString();

}

}

}

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingMathWorks.MATLAB.NET.Arrays;

usingtwice;

namespaceMatlabTest

{

publicpartialclassForm1 : Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender, EventArgs e)

{

inti = 5;

Twice c =newTwice();

MWArray result = c.twice((MWArray)i);

this.label2.Text = result.ToString();

}

}

}using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using MathWorks.MATLAB.NET.Arrays;

using twice;

namespace MatlabTest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

int i = 5;

Twice c = new Twice();

MWArray result = c.twice((MWArray)i);

this.label2.Text = result.ToString();

}

}

}

8、执行并查看结果

25550300_11.png

在代码中输入了一个变量i并为其赋值5,可以看到经过调用Matlab计算,顺利返回计算结果10。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity使用Matlab生成.dll文件的步骤如下: 1. 首先,确保你的计算机上已经安装了Matlab和Unity。 2. 在Matlab,编写你需要用于导出的函数代码。确保你的函数输入和输出参数是适用于Unity的数据类型,例如基本数据类型(int,float等)、数组或结构体。 3. 使用Matlab的“mcc”命令将你的Matlab代码编译成可执行文件,并同时生成C源文件。例如,在Matlab命令行输入: mcc -W lib:MyMatlabLib -T link:lib myMatlabFunction.m 这将生成一个名为“MyMatlabLib”(也可以自定义名称)的文件夹,并包含一个C源文件和其他必需的文件。 4. 打开Unity,在Assets文件夹下创建一个名为“Plugins”的文件夹。 5. 将之前生成的C源文件(通常是形如“MyMatlabLib.c”的文件)复制到“Plugins”文件。 6. 在Unity创建一个C#脚本,以调用你在Matlab编写并编译的函数。在脚本使用[DllImport]来导入.dll文件,并定义需要导入的函数签名。例如: [DllImport("MyMatlabLib")] private static extern int myMatlabFunction(int input1, float input2, ref float output); 7. 在Unity的任何可执行代码(例如Start或Update方法)使用刚刚定义的函数。例如: float outputValue = 0.0f; int result = myMatlabFunction(10, 5.0f, ref outputValue); 这将调用你在Matlab编写的函数,将输入参数(10和5.0)传递给函数,并将计算结果存储在outputValue变量。 8. 现在,你可以在Unity使用Matlab函数生成的.dll文件了。 需要注意的是,在使用Matlab生成.dll文件时,确保你的Matlab代码和Unity的项目具有相同的位数和平台(32位或64位)。 另外,还需要注意的是,.dll文件不能直接在移动设备上使用,因为移动设备不支持生成和使用.dll文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值