2024年4月29日发(作者:)

MATLAB R2008b和Visual C#2008混合编程教程

本文介绍如何用MATLAB R2008b和Visual C#2008混合编程。MATLAB和

Visual C#混合编程分为通过组件对象模型(COM)和通过DLL两种形式。

壹、 安装MATLAB Compiler Runtime v7.9

在MATLAB 安装目录(E:Program FilesMATLABR2008b)下找到

toolboxcompilerdeploy文件夹,在其win32(64位版本则是在win64)文件夹下

面找到。它就是MATLAB Complier Runtime(MCR)安装程序,

安装它。

安装后,在MCR安装目录(E:Program FilesMATLABMATLAB Compiler

Runtimev79)下找到runtime文件夹,win32(64位版本则是在win64)文件夹

下面找到,注册它。具体办法是在“开始菜单程序Microsoft Visual

Studio 2008Visual Studio Tools”中找到“Visual Studio 2008 命令提示”的快捷方式,

运行它。在这个命令行环境下,可以运行注册COM件的命令行程序。用法是:

cd "E:Program FilesMATLAB MATLAB Compiler Runtimev79runtimewin32"

E:

regsvr32

贰、 在MATLAB中编写程序

一、 编写基本程序

打开MATLAB R2008b,在工作目录下(D:My DocumentsMATLAB)新建

一个文件夹MyAdd。分别建立三个文件:

ExtAdd.m

function [ c ] = ExtAdd( a, b )

c=a+b;

ExtAddStr.m

function [ c ] = ExtAddStr( a,

b )

ExtAddStruct.m

function [ c ] =

ExtAddStruct( v )

end

c=[a b];

end

c.c=[v.a v.b];

c.v=v.v+1;

end

二、 配置MATLAB Builder NE

在MATLAB命令行窗口,运行mbuild设置MATLAB Builder编译程序:

>>mbuild -setup

Please choose your compiler for building standalone MATLAB applications:

Would you like mbuild to locate installed compilers [y]/n? n

Select a compiler:

[1] Lcc-win32 C 2.4.1

[2] Microsoft Visual C++ 6.0

[3] Microsoft Visual C++ .NET 2003

[4] Microsoft Visual C++ 2005

[5] Microsoft Visual C++ 2005 Express Edition

[6] Microsoft Visual C++ 2008

[0] None

Compiler: 6