使用 MATLAB 代码创建通用 COM 组件
支持的平台:Windows®
此示例说明如何使用 MATLAB® 函数创建通用 COM 组件并将其集成到应用程序中。目标系统不需要 MATLAB 的许可副本。
前提条件
验证您是否已安装 Windows 10 SDK 工具包。有关详细信息,请参阅 Windows 10 SDK。
验证您是否已安装 MinGW-w64。要从 MathWorks® 文件交换中心安装它,请参阅 MATLAB 对 MinGW-w64 C/C++ 编译器的支持。
为了确保 MATLAB 检测到 Windows 10 SDK 工具包和 MinGW-w64,请使用以下命令:
mbuild -setup -client mbuild_com
验证您是否已安装 Microsoft® Visual Studio®。
最终用户必须安装 MATLAB Runtime 才能运行应用程序。有关详细信息,请参阅下载并安装 MATLAB Runtime。
出于测试目的,您可以使用安装的 MATLAB 来代替 MATLAB Runtime。
在 MATLAB 中创建函数
在 MATLAB 中,检查您想要打包的 MATLAB 代码。对于此示例,打开位于 makesquare.m。matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\COM\MagicSquareExample\MagicSquareComp
function y = makesquare(x)
y = magic(x);在 MATLAB 命令提示符下,输入 makesquare(5)。
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9使用 compiler.build.COMComponent 创建 COM 组件
使用 compiler.build.comComponent 函数将该函数打包为 COM 组件。
将位于
makesquare.m文件复制到您的工作目录。例如,如果您使用的是 Visual Studio 版本 15,请输入:matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\COM\MagicSquareExample\MagicSquareCompcopyfile(fullfile(matlabroot,'toolbox','dotnetbuilder','Examples', ... 'VS15','COM','MagicSquareExample','MagicSquareComp','makesquare.m')); appFile = which('makesquare.m')
使用
compiler.build.comComponent函数构建 COM 组件。使用名称-值参量指定组件名称和类名。buildResults = compiler.build.comComponent(appFile, ... 'ComponentName','MagicSquareComp', ... 'ClassName','Class1');
您可以使用名称-值参量在
compiler.build命令中指定其他选项。有关详细信息,请参阅compiler.build.comComponent。compiler.build.Results对象buildResults包含有关编译类型、生成的文件、包含的支持包和编译选项的信息。该构建函数会在当前工作目录下的一个名为
MagicSquareCompcomComponent的文件夹中生成文件。有关生成的文件的信息,请参阅 打包 MATLAB 函数后生成的文件。注意
生成的组件不包括 MATLAB Runtime 或安装程序。要使用
buildResults对象创建安装程序,请参阅compiler.package.installer。
集成到 COM 应用程序中
要将 COM 组件集成到应用程序中,请参阅创建 Microsoft Visual Basic 工程。
另请参阅
compiler.build.comComponent | mcc