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

조회 수: 14 (최근 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