Matlab混合编程之引擎方式(C#篇)

跟上篇文章一样,也是用引擎的方式,但配置方法还是很不一样的。

配置环境:vs201364位)+Matlab2013a64位)+win8.164位)

Matlab安装路径:C:\Program Files\MATLAB\R2013a为例

 

1.添加引用:

一个是添加MWArray.dll的引用,路径为:C:\Program Files\MATLAB\R2013a\toolbox\dotnetbuilder\bin\win64\v2.0,

另一个是添加Matlab类型库的引用。在“引用”上右击“添加引用”,如下图

Matlab混合编程之引擎方式(C#篇)

 

2.加入命名空间:

using MathWorks;

using MathWorks.MATLAB;

using MathWorks.MATLAB.NET.Arrays;

using MathWorks.MATLAB.NET.Utility;

using MLApp;

3.示例代码:

一共尝试了四种方式,其中两种成功,两种失败,具体代码如下(加入到button事件里):

            引擎方式1,失败

            MLApp.MLAppClass matlab = new MLApp.MLAppClass();

            string command;

            command = "t=2:0.2:4*pi;y=sin(t);plot(t,y)";

            matlab.Visible = 1;

            matlab.Execute(command);

            command = @"print(gcf,   '-djpeg',   'c:\Test1')";

            matlab.Execute(command);

            pictureBox1.Image = Image.FromFile(@"c:\Test1.jpg");

 

            引擎方式2,失败

            MLApp.DIMLApp matlab = null;

            Type matlabAppType = System.Type.GetTypeFromProgID("Matlab.Application");

            matlab = System.Activator.CreateInstance(matlabAppType) as MLApp.DIMLApp;

            string command;

            command = "t=2:0.2:4*pi;y=sin(t);plot(t,y)";

            matlab.Visible = 1;

            matlab.Execute(command);

            command = @"print(gcf,   '-djpeg',   'c:\Test1')";

            matlab.Execute(command);

            pictureBox1.Image = Image.FromFile(@"c:\Test1.jpg");

 

            引擎方式3,成功

            MLApp.MLApp matlab = null;

            Type matlabAppType = System.Type.GetTypeFromProgID("Matlab.Application");

            matlab = System.Activator.CreateInstance(matlabAppType) as MLApp.MLApp;

            string command;

            command = "t=2:0.2:4*pi;y=sin(t);h = plot(t,y)";

            String path = Directory.GetCurrentDirectory();//获取当前路径

            matlab.Visible = 0;

            matlab.Execute(command);

            command = @"print(gcf,   '-djpeg',   '" + path + "\\Test1');close all";

            matlab.Execute(command);

            pictureBox1.Image = Image.FromFile(path + "\\Test1.jpg");

 

            引擎方式4,成功

            MLApp.MLApp O_matlab = new MLApp.MLApp();

            O_matlab.Visible = 0;

            string command1, command2;

            command1 = "t=0:0.01:2*pi;y=sin(t);h=plot(t,y)";//matlab脚本命令行

            String path = Directory.GetCurrentDirectory();//获取当前路径

            command2 = @"print(gcf,   '-djpeg',   '" + path + "\\Test1');close all";

            O_matlab.Execute(command1);

            O_matlab.Execute(command2);        // 执行Matlab命令

            O_matlab.Quit();

            O_matlab = null;

            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

            pictureBox1.Image = Image.FromFile(path + "\\Test1.jpg"); //读取图像

 

得到结果如下图:

Matlab混合编程之引擎方式(C#篇)

转自:http://blog.sina.com.cn/s/blog_6f7265cf0101nqhw.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值