Access ArgumentList and MATLAB Engine in User C++ Files - MATLAB & Simulink (original) (raw)
You can create MEX functions to passmatlab::mex::ArgumentList
ormatlab::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++ functiontask1
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 calltask1
using the passArgs
signature.
For this example, save the code in a file namedpassArgs.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 -I_matlabroot_/extern/include -L_matlabroot_/extern/bin/maca64 -L_matlabroot_/bin/maca64 -lmex -lMatlabDataArray
Save this code in callTasksMex.cpp
to call thetask1
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 thepassArgs.so
file.
mex -lpassArgs -L'pathPassArgs' callTasksMex.cpp
Run the function.
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 namedtask2.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 -I_matlabroot_/extern/include -L_matlabroot_/extern/bin/maca64 -L_matlabroot_/bin/maca64 -lmex -lMatlabDataArray
Save this code in callTaskMex.cpp
to call thepassEngine
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 thetask2.so
file.
mex -ltask2 -L'pathTask2' callTaskMex.cpp
Run the function.
Passing MATLABEngine to a shared object
See Also
matlab::mex::ArgumentList | matlab::engine::MATLABEngine