C#与Matlab混合编程入门

问题描述: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自身限制工具包被其他语言开发,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值