cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 6128|回复: 17

MATLAB2006a及更新版本下的M文件COM组件生成及其LabVIEW调用技术

[复制链接]
发表于 2007-11-15 22:18:55 | 显示全部楼层 |阅读模式
MATLAB2006a及更新版本下的M文件生成及其LabVIEW调用技术已经在2007年11月11日下午突破,文章和案例已经准备发布。

不知道对这个技术感兴趣的有没有??
下载文件请倒:http://www.cpubbs.com/blog/?1288/viewspace-3138.html
添加了一个例子:倒频谱函数的COM组件打包及测试

介绍了MATLAB2006a版本下COM组件的生成方法和详细过程,实现了LabVIEW下调用M文件生成的COM组件。实践证明,采用LabVIEW调用M文件生成的COM组件,完成混合编程,可以脱离MATLAB编程环境,能极大扩展M文件的应用范围,增强LabVIEW的功能,具有技术的应用和参考价值。



为了更大限度的使用已有的代码,并充分借助LabVIEW完成更高级的功能处理和数据分析,对MATLAB的COM生成技术和LabVIEW下对生成的COM组件调用进行了研究。传统上LabVIEW和MATLAB混合编程的两种方法是采用MATLAB script节点或者直接调用MATLAB ACTIVEX服务器的方法。采用MATLAB Script必须要安装了MATLAB软件;采用MATLAB ACTIVEX,同样需要安装MATLAB引擎;而如果将MATLAB M文件打包成COM组件,就可以只安装简单的驱动就可以脱离MATLAB编辑环境,和LabVIEW无缝结合起来运行。结果表明,可以完美的实现LabVIEW下对M文件的再利用,扩展了LabVIEW的功能。

[ 本帖最后由 wdyjz 于 2007-11-15 22:34 编辑 ]
 楼主| 发表于 2007-11-15 22:22:35 | 显示全部楼层
本技术内容已经形成一个论文投稿出去了,现在提前将技术内容给大家提供公开化参考,如有其他朋友再写同类的文章,意义就不大了,仅此提醒大家注意。
如果该技术的论文没有公开发表,那可能是因为我放弃发表的原因了,不过我希望将这个技术内容和经验同大家分享。

需要用到该技术的,可以参考。

需要更详细的论文文档和技术资料,再私下联系我吧。
发表于 2007-11-16 08:47:14 | 显示全部楼层
发表于 2007-11-16 10:21:26 | 显示全部楼层
嘿~~~前几天一直想matlab生成的dll如何在labview中使用,结果mxArray实在麻烦的很~~通过这个例子,发现原来还有另外一条路可以走,就是用com~~呵呵,不过速度好像不是很快啊,我在生成的com目录的distribute下,还是发现了ctf的文件,那么,是不是作为com的那个dll文件,实质上是一个wrapper?运行com还得需要从ctf文件中把函数给解压缩出来?如果真是如此,速度慢就是正常的很了~~~

PS:没有安装matlab的,请注意下,就是千万别把matlab安装在有空格的目录下,比如默认的X:\Program Files\Matlab71,在有的情况下,这样安装会出现问题的,我今天生成com的时候,就出错了-_-! ,结果还得把matlab删除,重新安装到E:\Matlab71下,麻烦的很~~~

[ 本帖最后由 飞雨天 于 2007-11-16 10:27 编辑 ]
 楼主| 发表于 2007-11-16 12:08:55 | 显示全部楼层
第一次加载会慢,但是一旦程序运行起来,就会很快了。把open 和close放在while loop外面,所有方法在while内进行即可。
发表于 2007-11-16 14:12:12 | 显示全部楼层
我在想,既然有了这个com,那么,为什么还要有那么多人去研究在labview中如何调用matlab的dll这样的问题呢?com的太简单了吗。难不成它还有什么缺点?(我指相对于dll调用而言)

而且在类似labview中支持activex自动化技术的,又是图形化编程的,用com绝对比dll来的简单
 楼主| 发表于 2007-11-17 20:39:56 | 显示全部楼层
这些各有各的好处,但以后这2个会逐渐退出大家的应用,取代它们的会是.NET技术,现在2006a和2006b提供了生成.NET控件的支持,可以真正的脱离matlab,但没有测试成功过。我将继续研究这个方面,其他朋友一起来吧。
 楼主| 发表于 2007-11-18 17:30:05 | 显示全部楼层

how to Create a .NET Componen

How to Create a .NET Component


To create a component you need to write M-code (or use existing code) and then create a project in MATLAB Builder for .NET that encapsulates the code.

Write, test, and save the MATLAB code to be used as the basis for the .NET component.

While you are still in MATLAB, issue the following command:
dotnettool


The MATLAB Builder dialog box appears.

In the MATLAB Builder dialog box, click File > New Project to open the New Project Settings dialog box.

Specify components and classes.

Add component names.

Add class names for classes that you want to create as part of the component.

Click OK to save the project.

Using the New Project Settings Dialog Box
Area of Dialog BoxHow to Use this AreaComponent nameType the name that you want to use for the component to be created. This name is also used for the assembly that contains the component.

A component name cannot match the name of any M-files or MEX-files added to the project.Class name

After you type the component name, the GUI automatically enters a default class name; the default is the component name with class appended.

For instance, the default class name for a component named MyComponentName would be MyComponentNameclass.

To change the name of the class, add a new class with the name that you want to use and remove the class that has the default name.

To add another class to your component, enter the class name in the Class name box, and click Add>>. The added class appears in the Classes list.

Any new classes become part of the newly built component. Removed classes are removed from the component on the subsequent build.Project versionDefault value is 1.0.

See Versions for additional information about version numbers.Project directory

Specifies where project and build files are written when compiling and packaging your components.

.NET Builder specifies the project directory automatically, based on the name of your current directory and the component name. You can accept the automatically generated project directory path or specify a different path. After you click OK in the Project Settings dialog box, the specified path is saved. If you decide to move the project or change anything on its path, you need to repeat the entire project specification process, including adding files to the project. Create a singleton MCR option Tells .NET Builder to generate code that shares a single instance of the MCR for each instance of the new component.


MCR stands for MATLAB Component Run-time, which is required to run MATLAB applications on machines that do not have the MATLAB desktop installed.
NOTE: .NET components always use a singleton MCR.Build Debug versionEnables you to trace back to the point where in your MATLAB code a failure occurred.

This setting does not affect M-file debugging.Display verbose outputTells .NET Builder to show each step in the build process. This output is saved in project_dir\build.log.




Use the Project Files pane in the MATLAB Builder dialog box to add one or more M-files that you want to encapsulate in each class.

Click Build > .NET Object.

The build process generates a C# wrapper class in the \src subdirectory of your project directory. It also creates a component assembly and .ctf file in the \distrib subdirectory of your project directory. The files in the \distrib directory define your .NET component.

The .ctf is a component technology file, which is required to support components that encapsulate MATLAB functions when running them on a user machine that does not have the MATLAB desktop installed.

Test the component and rebuild it as needed.

You probably want to test your component before using it in an application or before packaging it for use by others. After testing the component on your development platform, you can reopen the project if necessary and proceed to the next step.

Optionally, create a self-extracting executable containing the files that are required for development to a target machine.

Note   This step is necessary only if you want to make the component available to other application developers on a different target machine.

Click Component > Package Component to open the Package Files dialog box.

Add any user and system files that need to be included in your application.

Optionally include the MATLAB Component Runtime (MCR) if necessary.

Click Create to create a self-extracting executable, which is named componentname.exe.

See What Happens in the Package Process? for a list of the files in the package.

Distribute the self-extracting executable to the target machine.
 楼主| 发表于 2007-11-18 17:32:21 | 显示全部楼层
已经能将M文件生成.NET控件,但调用还没有实现。困难是参数输入和输出的数据类型问题,现在解决不了连线。
matlab的函数不声明变量类型,所以特别麻烦。
一般的C、C++做成的.NET控件参数输入输出类型都已经声明,所以很容易做。matlab的控件处理就比较麻烦了。
如图,我做了一个NET控件,函数声明的原型为:

function f=add(x,y)
f=x+y;
保存为:add.m
编译生成后缀为dll的.NET控件,加载后。如图,但数据类型特殊,无法实现连接。控件在压缩包里面,大家可以下载编程测试看看。

[ 本帖最后由 wdyjz 于 2007-11-18 18:22 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
 楼主| 发表于 2007-11-18 18:27:23 | 显示全部楼层
mathworks.matlab.Net.Array.MWArray 这个数据类型太变态了,搞不定如何转换。
 楼主| 发表于 2007-11-18 19:48:14 | 显示全部楼层

提供参考,看谁能最先解决这个问题

混合编程参数传递,有办法把vector转为mwarray吗?

我想从VC中传一个可变长度的数字序列给Matlab,看了帮助文档,只介绍了把数组变为mwarray来传递,可是数组是无法在执行时变长度的,就想用vector,可是好像没有转为mwarray的函数阿,怎么可以解决这个问题呢?
2006-12-27 18:34 SCIE
既然你已经知道了如何用一个普通的数组来构建mwArray变量,那么问题就在于如何从vector中获取vector中指向数据的数据指针。
::std::vector<double> a(6, 1);
double *pa = &(a[0]);
&(a[0]) 可以获得指向vector变量a中数据的指针,然后传递给mwArray的构造函数即可。

[code]
::std::vector<double> a(6, 1);
double *pa = &(a[0]);
mwArray C(2, 3, pa);

void CMxArrayDlg::OnButton1()  
{
// TODO: Add your control notification handler code here
mwArray A;
A=magic(3);
double *p;
p=mxGetPr(A.GetData());
CString str;
str.Format("%2.0f,%2.0f,%2.0f,%2.0f,%2.0f,%2.0f,%2.0f,%2.0f,%2.0f",
  p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8]);
SetDlgItemText(IDC_EDIT1,str);
  
}

[ 本帖最后由 wdyjz 于 2007-11-18 19:59 编辑 ]
 楼主| 发表于 2007-11-18 20:57:33 | 显示全部楼层

网上搜索的数据类型的定义和使用

mwArray和mxArray有什么区别?

mxArray是个结构体,而mwArray是个C++类。首先,mxArray是Matlab C 函数库的结构体,而mwArray是Matlab C++ 函数库中对mxArray的包装类。
其次,二者的内存管理方式不同。mxArray的内存管理方式比较松散,由于是C函数库,没有数据封装,必须对临时阵列和约束阵列的概念极为明确,并且须小心地防止内存泄漏(要多写好多代码)。尽管有自动内存管理机制(mlfEnterNewContext, mlfReleasePreviousContext),仍然要处处调用mlfAssign,麻烦得很。然而mwArray就好的多,一切交给C++对象去搞定,你只要放心地用就可以了。不过Matlab C++函数库为了防止频繁内存分配和释放重写了内存分配和释放等函数。你会发现对于mwArray a,b; a=b;实际上并没有生成两个相同数据块,只是指针,只有发成数据改变时才copy完整数据。
再次,这两个东西各自有一套与之相对应的函数。函数的返回值类型不同,需要多加注意。
最后,用的时候mxArray要使用指针,而mwArray直接当类对象使。如果你不是Hardcore级的牛人或具有Hardcore倾向的潜牛人,我强烈建议你使用后者!举一个简单的例子:如果你需要计算c=a+b那么两者的区别是这样的:

mxArray:
void fun(){
    ……
    double d_a=1,d_b=2;
    mxArray *a,*b,*c;
    mlfEnterNewContext(0,0);
    mlfAssign(&a,mlfScalar(d_a));
    mlfAssign(&b,mlfScalar(d_b));
    mlfAssign(&c,mlfPlus(a,b));
    ……
    mlfReleasePreviousContext(0,0);
    mxDestroyArray(a);
    mxDestroyArray(b);
    mxDestroyArray(c);
}

mwArray:
void fun(){
    ……
    mwArray a,b,c;
    a=1;
    b=2;
    c=a+b;
    ……
}
发表于 2007-11-19 09:42:28 | 显示全部楼层
呵呵,把这个帖子打造成matlab混编第一贴吧~~~顶下~~
发表于 2009-3-3 09:49:10 | 显示全部楼层
发表于 2009-3-10 22:56:03 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2025-4-7 17:44 , Processed in 1.543549 second(s), 7 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表