Set and Get MATLAB Variables from MEX - MATLAB & Simulink (original) (raw)
MEX functions can put variables into the MATLABĀ® base and global workspaces during MEX function execution. MEX functions can get variables from the MATLAB base and global workspaces during MEX function execution.
To put variables in the MATLAB base or global workspace from MEX functions use the setVariable function.
To get variables from the MATLAB base or global workspace and bring them into a MEX function, use the getVariable function.
Get Variable from MATLAB Workspace
Suppose that there is a variable named result
in the MATLAB base workspace. This variable is of typedouble
.
result = 1^3 + 5^3 + 3^3;
To get the result
variable from the MATLAB workspace, call getVariable
, which returns the variable as a matlab::data::Array
.
#include "mex.hpp"
#include "mexAdapter.hpp"
using matlab::mex::ArgumentList;
using namespace matlab::engine;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
ArrayFactory factory;
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
Array result = matlabPtr->getVariable(u"result");
double mexResult = std::move(result[0]);
}
};
The result
variable is a shared copy of theresult
variable in the MATLAB workspace. Using std::move
when assigningresult
in the MEX function to the nativedouble
variable mexResult
unshares the value with the variable in the MATLAB workspace.
Put Variable in MATLAB Workspace
MEX functions can put variables in the MATLAB base or global workspace. If a variable with the same name exists in the specified workspace, setVariable
overwrites it.
For example, you can make a variable available from the MATLAB global workspace so that any MATLAB function can define this global variable.
This MEX function creates a global variable called mexGlobal
. The value of this variable is 153
.
#include "mex.hpp"
#include "mexAdapter.hpp"
using matlab::mex::ArgumentList;
using namespace matlab::engine;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
ArrayFactory factory;
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
Array val = factory.createScalar(153.0);
matlabPtr->setVariable(u"mexGlobal", val, WorkspaceType::GLOBAL);
}
};
To access the global variable from MATLAB functions, use the global keyword to define the variable as global in the function workspace.
function testMexGlobal global mexGlobal localVar = 1^3 + 5^3 + 3^3; if localVar == mexGlobal disp('Global found') end end
Global variables are shared among all functions that declare the variable as global. Any change of value to that variable, in any function, is visible to all the functions that declare it as global.
Set and Get Variable Example Code
For a complete example that uses getVariable, download these two files and follow the instructions in the files to build and run the MEX function.
mexgetarray.cpp and mexgetarray.hpp.