Clear Filters
Clear Filters

using matlab::en​gine::MATL​ABEngine from MexFunction::operator() to call the "path" matlab function

1 view (last 30 days)
From the Matlab Command Window, calling for path works normally.
From a C++-mexFunction I try to do the same.
Doing so, I have this error :
void MexFunction::operator()(mm::ArgumentList outputs, mm::ArgumentList inputs)
{
auto matlabptr = getEngine();
matlab::data::ArrayFactory f;
auto emptyArray = f.createScalar("");
auto path = matlabptr->feval(matlab::engine::convertUTF8StringToUTF16String("path"),emptyArray);
if (path.getNumberOfElements() > 0) {
std::string myPath = matlab::data::TypedArray<matlab::data::MATLABString>(path)[0];
std::cout << myPath << std::endl;
}else{
std::cout << "Rate" <<std::endl;
}
}
Where do I goof ?
-----------------------
Edit and solve
path with one argument written this way in the `Command Window` will generate this error. I wanted to call it with no arguments, thus I had to do the following
void MexFunction::operator()(mm::ArgumentList outputs, mm::ArgumentList inputs)
{
md::TypedArray<md::MATLABString> values = inputs[0];
std::cout << "Going to run " << (std::string)values[0] <<std::endl;
auto matlabptr = getEngine();
matlab::data::ArrayFactory f;
auto emptyArray = f.createEmptyArray();
auto res = matlabptr->feval((std::string)values[0],1, {}); // <- this is the correct interface to call
auto tmp = md::CharArray(res[0]);
std::cout << tmp.toAscii() << std::endl;
}

Answers (1)

Vincent Huber
Vincent Huber on 24 Sep 2020
This is the correct call to feval for command with no arguments:
auto res = matlabptr->feval((std::string)values[0],1, {});

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!