How can I call a matlab function that takes no arguments using matlab engine C++ API?

8 次查看(过去 30 天)
I have a matlab function (myFunc) that takes no arguments and returns a matlab string. I want to call this function using the matlab engine C++ API. In all examples I have found in documentation, the functions used receive at least one argument (like sqrt o gcd). I have tried declaring an empty variable and using it like this:
const matlab::data::Array arg;
auto res = matlabPtr->feval(u"myFunc", arg);
The code compiles OK, but when I run it I get the exception error "Default constructed arguments are not allowed"
Is there a way to call this kind of function?
Thank you for your help
  3 个评论
Fabián Lema
Fabián Lema 2024-8-15
Yes. It gives a compile error : error C2661: 'matlab::execution::ExecutionInterface::feval': no overloaded function takes 1 arguments
dpb
dpb 2024-8-15
I don't know anything about the toolbox and a quick look at the doc didn't answer the question, but the simple workaround would be to just pass an argument but ignore it inside the function.
If nobody who uses the toolbox that knows a real answer comes by shortly, I'd suggest submit an official support request; the Answers forum is not that...

请先登录,再进行评论。

回答(1 个)

Anshuman
Anshuman 2024-8-20
When using the MATLAB Engine C++ API to call a MATLAB function that takes no arguments, you should not pass an empty Array as an argument. Instead, you should call the function with an empty argument list. Use the 'feval' method without passing any arguments, except for the function name and the number of output arguments you expect.
Here's how you can do it:
auto results = matlabPtr->feval(u"myFunc", 0, std::vector<matlab::data::Array>());
As per my understanding , by following this approach you should be able to call MATLAB functions with no input arguments using the MATLAB Engine C++ API.
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Data Exchange and Mapping with C++ Applications 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by