如何将Matlab类编译成C lib?

Sol*_*ong 4 matlab matlab-deployment matlab-compiler

这个问题的起源是从这里如何在c中调用的matlab函数中使用"全局静态"变量.

我试图将"全局变量"封装到一个对象中.但是我不知道如何使用MATLAB Compiler(mcc)将matlab类导出到c ++

为此,我尝试了标准命令

Matlab命令

mcc -W cpplib:Vowel4 -T link:lib Vowel4.m
Run Code Online (Sandbox Code Playgroud)

Matlab脚本

classdef Vowel4

  properties
    x
    y
  end

  methods
    Vowel4
    A
    B
  end

end
Run Code Online (Sandbox Code Playgroud)

生成的lib实际上是独立的函数而不是c ++类.

如何将Matlab中的类编译成c ++类?

我一直在寻找答案,但没有找到答案.

显然matlab命令不适合这种情况.但是我找不到有关将matlab类构建到c ++类中的任何信息.

==========================编辑======================= =

实际的cpp代码如下:@Alan

mclInitializeApplication(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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这里matlabA()对应于loadData(),它加载数据,soundByCoef(F)对应于matlabB(),它在主循环中完成工作.

Amr*_*mro 6

正如Alan所说,只是建议使用handle类作为全局变量的容器(这样的对象将通过引用传递).创建的对象不是由C++代码直接操作的(它将存储在通用的mxArray/mwArrayC/C++结构中).

我所知,你不能直接建立使用MATLAB编译共享库时编译classdef风格MATLAB类为适当的C++类.它只支持构建常规功能.您可以为MATLAB类成员方法创建功能接口,但这是一个不同的故事......

也许一个完整的例子可以帮助说明我的想法.首先让我们在MATLAB端定义代码:

GlobalData.m

这是用于存储全局变量的句柄类.

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
Run Code Online (Sandbox Code Playgroud)

create_globals.m

一个包装函数,充当上述类的构造函数

function globals = create_globals()
    %CREATE_GLOBALS  Instantiate and return global state

    globals = GlobalData();
    globals.val = 2;
end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

将上述文件存储在当前目录中,让我们使用MATLAB编译器构建C++共享库:

>> mkdir out
>> mcc -W cpplib:libfoo -T link:lib -N -v -d ./out create_globals.m fcn_add.m fcn_times.m
Run Code Online (Sandbox Code Playgroud)

您应该期望以下生成的文件(我在Windows机器上):

./out/libfoo.h
./out/libfoo.dll
./out/libfoo.lib
Run Code Online (Sandbox Code Playgroud)

接下来,我们可以创建一个示例C++程序来测试库:

main.cpp中

// 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);
}
Run Code Online (Sandbox Code Playgroud)

让我们构建独立程序:

>> mbuild -I./out main.cpp ./out/libfoo.lib -outdir ./out
Run Code Online (Sandbox Code Playgroud)

最后运行可执行文件:

>> 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 
Run Code Online (Sandbox Code Playgroud)

HTH