Main Content

Access ArgumentList and MATLAB Engine in User C++ Files

You can create MEX functions to pass matlab::mex::ArgumentList or matlab::engine::MATLABEngine arguments to shared library functions. (since R2024b)

Use matlab::mex::ArgumentList to Pass MATLAB Arguments to Library Functions

In this example, you want to pass MATLAB® data to a library function. Write a C++ function task1 to validate the input arguments, call the library function to manipulate the data, then return the data in the output arguments. You can create a generic header file passArgs.h to define the MATLAB arguments as matlab::mex::ArgumentList&. Then you can write a MEX function callTasksMex.cpp to call task1 using the passArgs signature.

For this example, save the code in a file named passArgs.cpp.

#include "mex.hpp"
#include "passArgs.h"
#include <vector>
 
void task1(std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr,
    matlab::mex::ArgumentList& outputs, 
    matlab::mex::ArgumentList& inputs) {

    matlab::data::ArrayFactory factory;

    // Examine inputs
    matlabPtr->feval(u"disp", 0, 
        std::vector<matlab::data::Array>({ factory.createScalar("Hello World") }));
    matlab::data::TypedArray<double> inArray = inputs[0];
    const double inScalar = inputs[1][0];
    matlab::data::CharArray inChar = inputs[2];
    // Call library function to manipulate inputs and return data in outputs
    result = //...
    outputs[0] = result;
}

Save this code in a header file named passArgs.h.

#pragma once
 
void passArgs(std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr, 
    matlab::mex::ArgumentList& outputs, 
    matlab::mex::ArgumentList& inputs);

Compile this code on macOS with Apple silicon. Replace matlabroot with the value returned by matlabroot. For information about building on other platforms, see Requirements to Build C++ Engine Applications.

g++ -std=c++11 -c passArgs.cpp -o passArgs.o -I matlabroot/extern/include 
-L matlabroot/extern/bin/maca64 -L matlabroot/bin/maca64 -lmex -lMatlabDataArray -fpic

Create the shared library file passArgs.so.

g++ -std=c++11 -shared passArgs.o -o passArgs.so -Imatlabroot/extern/include 
-Lmatlabroot/extern/bin/maca64 -Lmatlabroot/bin/maca64 -lmex -lMatlabDataArray

Save this code in callTasksMex.cpp to call the task1 function.

#include "mex.hpp"
#include "mexAdapter.hpp"
#include "passArgs.h"
 
class MexFunction : public matlab::mex::Function {
public:
    void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
        auto ml = getEngine();
        task1(ml,outputs,inputs);
 
    }
};

Build the callTasksMex MEX file, linking to the shared library. Replace pathPassArgs with the full path to the passArgs.so file.

mex -lpassArgs -L'pathPassArgs' callTasksMex.cpp

Run the function.

callTasksMex(2)
Hello World
Input is greater than 0

Pass matlab::engine::MATLABEngine Objects to Library Functions

In this example, you want to pass a matlab::engine::MATLABEngine object to a library function.

For this example, save the code in a file named task2.cpp.

#include "mex.hpp"
#include "passEngine.h"
 
void task2(std::shared_ptr<matlab::engine::MATLABEngine> ml) {
    ml->feval(u"disp","Passing MATLABEngine to a shared object");
    // Pass MATLABEngine object to library function
}

Save this code in a header file named passEngine.h.

#pragma once
 
void passEngine(std::shared_ptr<matlab::engine::MATLABEngine> ml);

Compile this code on macOS with Apple silicon. Replace matlabroot with the value returned by matlabroot. For information about building on other platforms, see Requirements to Build C++ Engine Applications.

g++ -std=c++11 -c task2.cpp -o task2.o -I matlabroot/extern/include 
-L matlabroot/extern/bin/maca64 -L matlabroot/bin/maca64 -lmex -lMatlabDataArray -fpic

Create the shared library file task2.so.

g++ -std=c++11 -shared task2.o -o task2.so -Imatlabroot/extern/include 
-Lmatlabroot/extern/bin/maca64 -Lmatlabroot/bin/maca64 -lmex -lMatlabDataArray

Save this code in callTaskMex.cpp to call the passEngine function.

#include "mex.hpp"
#include "mexAdapter.hpp"
#include "passEngine.h"
 
class MexFunction : public matlab::mex::Function {
public:
    void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
        auto ml = getEngine();
        passEngine(ml);
 
    }

Build the callTaskMex.cpp MEX file, linking to the shared library. Replace pathTask2 with the full path to the task2.so file.

mex -ltask2 -L'pathTask2' callTaskMex.cpp

Run the function.

callTaskMex
Passing MATLABEngine to a shared object

See Also

|

Related Topics