How to compile Matlab class in C lib?

The origin of this question is from here. How to use the "global static" variable in the matlab function called in c .

I am trying to encapsulate a "global variable" in an object. However, I don't know how to export the matlab class in C ++ using MATLAB Compiler (mcc)

For this, I just tried the standard command

Matlab Team

mcc -W cpplib:Vowel4 -T link:lib Vowel4.m

Matlab script

classdef Vowel4

  properties
    x
    y
  end

  methods
    Vowel4
    A
    B
  end

end

The generated lib is actually stand-alone functions, not a C ++ class.

How can I compile classes in matlab into c ++ classes?

I searched for the answer, but did not find it.

Obviously, the matlab command is not suitable for this scenario. However, I cannot find any information on creating Matlab classes in C ++ classes.

======================= ==================== ====

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);
    }
}

matlabA() loadData(), , soundByCoef (F) matlabB(), .

+4
2

, , ( , ), ++ ( mxArray/mwArray C/++).

, , MATLAB classdef ++ MATLAB Compiler. . 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

create_globals.m

,

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

    globals = GlobalData();
    globals.val = 2;
end

fcn_add.m, fcn_times.m

MATLAB, ++

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:

>> mkdir out
>> mcc -W cpplib:libfoo -T link:lib -N -v -d ./out create_globals.m fcn_add.m fcn_times.m

( Windows):

./out/libfoo.h
./out/libfoo.dll
./out/libfoo.lib

++ :

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);
}

:

>> 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 

+6

, , , , .

classdef Foo < handle
  properties
    value
  end

  methods
    function obj = Foo(value)
      obj.value = value;
    end
  end
end

: Foo handle, , . . .

function foo = matlabA()
  foo = new Foo(1);
end

function matlabB(foo)
  foo.value
end

, matlab , MATLAB Component Runtime - c/++.

matlab c/++; MCR . , , , matlab script ( : c/++ script) , matlab.

: ++ Matlab ++ , . , ++ mexFunction . , , Matlab, , mexGetVariablePtr, - . , , , , mexFunction.

, ++, Matlab, Matlab; , - , script , .

, ++ , , MATLAB , " ", mexFunction, Matlab :

data = loadData();
while(~stop) {
  position = getFingerPositionMex();
  soundByCoef(position, data);
}

, soundByCoef, Matlab pass by reference, .

+2

All Articles