问题描述:vs2008 调用matlab dll时提示类型初始值设定项引发异常。
解决方法:m文件及生成dll文件的目录不要有中文字段,在含有中文字段的目录下,会引起上述问题。
问题描述:vs2008 调用matlab dll时提示类型初始值设定项引发异常。
解决方法:m文件及生成dll文件的目录不要有中文字段,在含有中文字段的目录下,会引起上述问题。
重要提示: 由于MATLAB自身限制工具包被其他语言开发,C# C C++在调用工具包的时候容易出错,或是根本无法编译调用,建议依赖MATLAB工具箱开发的朋友慎用!如果仅仅是利用MATLAB计算,问题不大。测试已成功!
掉转方向,直攻MATLAB GUI!!!
C#与MATLAB混合编程初步
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using last;
using MathWorks;
using MathWorks.MATLAB;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using System.IO;
namespace LastTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LastClass st = new LastClass();
//int h = 1, D = 2;
//st.mathplot(1, 2);
string htex, dtex, ntex, xlab,ylab ;
htex = textBox1.Text;
dtex = textBox2.Text;
ntex = textBox3.Text;
xlab = textBox4.Text;
ylab = comboBox1.Text;
double h = Convert.ToDouble(htex);
double d = Convert.ToDouble(dtex);
MWArray mt_a = (MWArray)h;
MWArray mt_d = (MWArray)d;
MWArray mt_c = (MWArray)ntex;
MWArray mt_xlab = (MWArray)xlab;
MWArray mt_ylab = (MWArray)ylab;
st.mathplot((MWNumericArray)mt_a, (MWNumericArray)mt_d, (MWCharArray)mt_c, (MWCharArray)mt_xlab, (MWCharArray)mt_ylab);
//string command1;
String path = Directory.GetCurrentDirectory();
path = path + "\\image\\test1.jpg";
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(path); //读取图像
}
}
}
mathplot.m文件。
function mathplot(h,D,xlabel1,ylabel1,title1)
for x=-10:0.1:10,
if x>D
y=h;
hold on;
xlabel(xlabel1);
ylabel(ylabel1);
title(title1);
plot(x,y);
elseif x<-D
y=-h;
hold on;
xlabel(xlabel1);
ylabel(ylabel1);
title(title1);
plot(x,y);
else
y=h/(D*x);
hold on;
xlabel(xlabel1);
ylabel(ylabel1);
title(title1);
plot(x,y);
set(gcf,'color',[1,1,1]); % 背景色设置为白色
%mkdir image;
% 在当前文件夹下新建image文件夹,如果已存在会warning,不影响运行
% ========================
saveas(gcf,['image','\\test1.jpg'])
end
end
重要提示:
掉转方向,直攻MATLAB GUI!!!
C#与MATLAB混合编程初步
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using last;
using MathWorks;
using MathWorks.MATLAB;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using System.IO;
namespace LastTest
{
}
mathplot.m文件。
function mathplot(h,D,xlabel1,ylabel1,title1)
for x=-10:0.1:10,
end