c#(vs2013)、 matlab(2014a)混合编程

来源:互联网 发布:手机图文排版软件 编辑:程序博客网 时间:2024/04/30 01:07

今天试着用c#和matlab混合编程,折腾了半天终于弄成功,记录一下。
1、在Command Window窗口中输入mbuild –setup查看编译器,将编辑器设置为vs
2、安装MCRinstaller.exe,在matlab安装目录下toolbox\compiler\deploy\win64
3、注册mwcomutil.dll,在\bin\win64中
4、生成add.m文件

function [c]=add(a,b)c=a+b;end
5、在Command Window窗口中输入deploytool,生成dll

这里写图片描述
这里写图片描述
若成功:
这里写图片描述
若出现Matlab Compiler__Test checkout of feature ‘Compiler’ failed参考http://www.ilovematlab.cn/thread-315935-1-1.html
6、c#引用dll

class Program    {        static void Main(string[] args)        {            Class1 a = new Class1();            MWArray b = 3, c = 6;            MWArray result = a.add(b, c);            Console.WriteLine(result.ToString());            Console.ReadKey();        }    }

引用生成的dll和toolbox\dotnetbuilder\bin\win64\v2.0下的MWArray.dll,再运行
若出现TypeInitializationException(类型初 始值设定项引发异常),参考http://www.cnblogs.com/machine/p/3829249.html
默认X86,换为X64
成功

0 0