水木年华zzu 发表于 2009-8-10 15:01

matlab 与.net混合编程

1.建一个m文件(test.m) 如
function test(h,D)
for x=-10:0.1:10,
   if x>D
       y=h;
       hold on;
       plot(x,y)
   elseif x<-D
       y=-h;  
       hold on;
       plot(x,y)
   else
       y=h/(D*x);
       hold on;
       plot(x,y)
   end
end

2.在matlab中输入comtool命令,就打开了matlab com builder,

点击file-new project,新建一个工程,

在component name里输入名字 点击ok就行了。
然后点击project--Add files,将test.m添加入工程,

然后点Build-Com Object,就会在comtest\distrib\文件夹下生成一个test.dll(它就
是做好的com组件)。
3.c#下实现调用
新建一个c#项目(我采用的是vs.net2008),选中右边的解决方案资源管理器中的引用
,点鼠标右键,选添加引用,在弹出来的窗口中选com,然后也找到test .dll,点选
择,然后确定就可,此时此com组件也添加到工程里面去了。 还需要将MWArray.dll添加引用,
MWArray.dll在matlab\toolbox\donetbuilder\bin\win32\MWArray.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            MWNumericArray m = 2.0;
            MWNumericArray n = 1.0;
            mytest.mytestclass ts = new mytest.mytestclass();
            //test.testclass ts = new test.testclass();
            //MWArray result = ts.test(f);
           // ts.test((MWNumericArray) m, (MWNumericArray) n);
            ts.test(m, n);
            Console.ReadLine();
           
        }
    }
}
就可以看到matlab画出的图形了

starbinbin 发表于 2009-8-11 15:14

好牛逼啊!
页: [1]
查看完整版本: matlab 与.net混合编程