如何将Matlab类编译成C库?
这个问题的由来就在这里 我试图将“全局变量”封装到一个对象中。但是我不知道如何使用将MATLAB类导出到C++中。 要做到这一点,我只是尝试了标准命令 Matlab命令 Matlab脚本如何将Matlab类编译成C库?,matlab,matlab-deployment,matlab-compiler,Matlab,Matlab Deployment,Matlab Compiler,这个问题的由来就在这里 我试图将“全局变量”封装到一个对象中。但是我不知道如何使用将MATLAB类导出到C++中。 要做到这一点,我只是尝试了标准命令 Matlab命令 Matlab脚本 生成的LIB实际上是独立函数,而不是C++类。 如何将Matlab中的类编译成C++类?< /P> 我一直在寻找答案,但没有找到 显然,matlab命令不适合这种情况。但是,我找不到任何关于将MATLAB类构建到C++类中的信息。 =====================================编辑=
生成的LIB实际上是独立函数,而不是C++类。
如何将Matlab中的类编译成C++类?< /P> 我一直在寻找答案,但没有找到 显然,matlab命令不适合这种情况。但是,我找不到任何关于将MATLAB类构建到C++类中的信息。 =====================================编辑======================== 实际cpp代码如下:@AlanmclInitializeApplication(NULL, 0);
loadDataInitialize();
soundByCoefInitialize();
loadData();
mwArray F(4, 1, mxDOUBLE_CLASS);
float test[4];
for ( ;; ){
const Frame frame = controller.frame();
const FingerList fingers = frame.fingers();
if ( !fingers.empty() ){
for ( int i = 0; i < 4; i ++ ){
double v = fingers.count() > i ? (fingers[i].tipPosition().y / 50) - 2 : 0;
F(i+1,1) = v;
test[i] = v;
cout << v << ' ';
}
cout << endl;
soundByCoef(F);
}
}
mclinitializapplication(NULL,0);
loadDataInitialize();
soundByCoefInitialize();
loadData();
MWF阵列(4,1,MXU类);
浮球试验[4];
对于(;;){
const Frame=controller.Frame();
const FingerList fingers=frame.fingers();
如果(!fingers.empty()){
对于(int i=0;i<4;i++){
双v=fingers.count()>i?(fingers[i].tiposition().y/50)-2:0;
F(i+1,1)=v;
试验[i]=v;
cout根据中的线程,建议不是将函数封装在类中,而是使用类来传递全局变量,而编译会使您无法使用全局变量
classdef Foo < handle
properties
value
end
methods
function obj = Foo(value)
obj.value = value;
end
end
end
据我所知,matlab编译器并不编译代码本身,而是编写一些包装函数来处理从c/c++对代码调用所述运行时
我建议避免在matlab和c/c++之间来回跳太多;转换数据类型和调用MCR肯定会有一些开销。我真正使用它的目的是包装一个复杂但自包含的matlab脚本(即:不需要通过所述脚本中途与c/c++代码交互)作为一个函数,或者打包代码,以便部署到没有matlab完整副本的环境中
<强>作为一个有趣的旁注:如果你从Matlab中的C++调用了/C++,并且C++代码需要访问全局变量,事情就简单多了。你可以简单的把C++代码打包成一个编译它。在需要访问MATLAB工作空间中的变量的地方,你可以使用它将返回指向数据的只读指针。您正在访问的变量可以位于全局工作区中,也可以位于调用mexFunction的函数的工作区中
使用这种方法,我建议对C++和Matlab代码中的变量进行自由注释,因为它们之间的链接可能不是从MATLAB方面明显的;你不希望有人稍后出现,编辑脚本并想知道它为什么会被破坏。
<强>在这种情况下,C++侧似乎不需要访问数据,因此可以重构它,使Matlab通过将“获取当前手指位置”代码封装到MEX函数中进行调用,然后使Matlab进行循环:
data = loadData();
while(~stop) {
position = getFingerPositionMex();
soundByCoef(position, data);
}
假设您不修改soundByCoef
Matlab中的数据,则将使用通过引用传递,因此不会复制大型数据集。正如Alan所指出的,仅建议使用handle类作为全局变量的容器(这样的好处是通过引用传递对象)创建的对象不打算直接由C++代码操作(它将存储在通用代码> MxReals/MWARLYE/CONTRO/C/C++结构中)。
AS,当使用MATLAB编译器构建共享库时,不能直接编译CythDeF风格的MATLAB类到C++类中。它只支持建立正则函数。可以创建函数类接口到Matlab类成员方法,但这是另一个故事…
也许一个完整的例子有助于说明我的想法。首先让我们在MATLAB端定义代码:
全球数据
这是用于存储全局变量的句柄类
classdef GlobalData < handle
%GLOBALDATA Handle class to encapsulate all global state data.
%
% Note that we are not taking advantage of any object-oriented programming
% concept in this code. This class acts only as a container for publicly
% accessible properties for the otherwise global variables.
%
% To manipulate these globals from C++, you should create the class API
% as normal MATLAB functions to be compiled and exposed as regular C
% functions by the shared library.
% For example: create(), get(), set(), ...
%
% The reason we use a handle-class instead of regular variables/structs
% is that handle-class objects get passed by reference.
%
properties
val
end
end
fcn_add.m,fcn_times.m
Matlab函数作为C++函数
function out = fcn_add(globals, in)
% receives array, and return "input+val" (where val is global)
out = in + globals.val;
end
function out = fcn_times(globals, in)
% receives array, and return "input*val" (where val is global)
out = in .* globals.val;
end
使用当前目录中存储的上述文件,使用Matlab编译器构建C++共享库:
>> mkdir out
>> mcc -W cpplib:libfoo -T link:lib -N -v -d ./out create_globals.m fcn_add.m fcn_times.m
除其他文件外,您应该会看到以下生成的文件(我在Windows计算机上):
接下来,我们可以创建一个C++程序来测试库:
main.cpp
最后运行可执行文件:
>> cd out
>> !main
Added matrix:
3 6 9
4 7 10
5 8 11
Multiplied matrix:
2 8 14
4 10 16
6 12 18
Hth实际上,在我的应用中,我在一个MATLAB的声音产生脚本中使用C++ SDK。我必须从磁盘读取巨大的数据。所以Matlab()要加载数据,在应用程序中我只调用Matlab()在主循环中,没有一次又一次地加载数据。这就是为什么我要声明全局变量。等等,你说你在Matlab中运行这个程序,那就是:Matlab脚本调用C++代码,C++调用更多的MATLAB来获取一些数据。好主意,在Matlab中考虑调用C++ SDK,但是这里不是这样。如果你仍然有问题,你能提供更多关于你的项目的上下文吗?谢谢。这似乎归结为有一组数据需要在MATLAB方面持久,所以我认为句柄类方法是你的最佳选择。e不能只使用全局函数,因为如果我们在两个线程中调用此函数,它们会产生干扰;因此,使用handle类方法可以让您将数据存储在matlab内存空间中,同时对函数的特定实例进行引用。您真的很有帮助,也很热心,我非常感谢您的努力我很抱歉不理解你为什么建议使用handle类。我以为你建议使用一个类来共享变量betw
function globals = create_globals()
%CREATE_GLOBALS Instantiate and return global state
globals = GlobalData();
globals.val = 2;
end
function out = fcn_add(globals, in)
% receives array, and return "input+val" (where val is global)
out = in + globals.val;
end
function out = fcn_times(globals, in)
% receives array, and return "input*val" (where val is global)
out = in .* globals.val;
end
>> mkdir out
>> mcc -W cpplib:libfoo -T link:lib -N -v -d ./out create_globals.m fcn_add.m fcn_times.m
./out/libfoo.h
./out/libfoo.dll
./out/libfoo.lib
// Sample program that calls a C++ shared library created using
// the MATLAB Compiler.
#include <iostream>
using namespace std;
// include library header generated by MATLAB Compiler
#include "libfoo.h"
int run_main(int argc, char **argv)
{
// initialize MCR
if (!mclInitializeApplication(NULL,0)) {
cerr << "Failed to init MCR" << endl;
return -1;
}
// initialize our library
if( !libfooInitialize() ) {
cerr << "Failed to init library" << endl;
return -1;
}
try {
// create global variables
mwArray globals;
create_globals(1, globals);
// create input array
double data[] = {1,2,3,4,5,6,7,8,9};
mwArray in(3, 3, mxDOUBLE_CLASS, mxREAL);
in.SetData(data, 9);
// create output array, and call library functions
mwArray out;
fcn_add(1, out, globals, in);
cout << "Added matrix:\n" << out << endl;
fcn_times(1, out, globals, in);
cout << "Multiplied matrix:\n" << out << endl;
} catch (const mwException& e) {
cerr << e.what() << endl;
return -1;
} catch (...) {
cerr << "Unexpected error thrown" << endl;
return -1;
}
// destruct our library
libfooTerminate();
// shutdown MCR
mclTerminateApplication();
return 0;
}
int main()
{
mclmcrInitialize();
return mclRunMain((mclMainFcnType)run_main, 0, NULL);
}
>> mbuild -I./out main.cpp ./out/libfoo.lib -outdir ./out
>> cd out
>> !main
Added matrix:
3 6 9
4 7 10
5 8 11
Multiplied matrix:
2 8 14
4 10 16
6 12 18