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

7 vues (au cours des 30 derniers jours)
Fabián Lema
Fabián Lema le 14 Août 2024
Réponse apportée : Anshuman le 20 Août 2024
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 commentaires
Fabián Lema
Fabián Lema le 15 Août 2024
Yes. It gives a compile error : error C2661: 'matlab::execution::ExecutionInterface::feval': no overloaded function takes 1 arguments
dpb
dpb le 15 Août 2024
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...

Connectez-vous pour commenter.

Réponses (1)

Anshuman
Anshuman le 20 Août 2024
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!

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by