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

1 次查看(过去 30 天)
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;
}

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from C++ 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by