Function With varargin Argument That Can Be Compiled With codegen

조회 수: 10 (최근 30일)
Hi Community,
What is the recipe to defining a function with a varargin argument that can be compiled with codegen?
Here is my example that doesn't work:
function test_functionArgument_varargin(varargin) %#codegen
assert( all(size(varargin)<= [1 , 3])); assert(isa(varargin, 'cell' ));
number = double(varargin{1});
string = varargin{2};
disp(number);
disp(string);
end
And this is an example of the function's call output (without compiling the function):
test_functionArgument_varargin(1,"AAAA",55)
1 AAAA
Executing
codegen test_functionArgument_varargin
produces these errors that have a quite cryptic message at least to me:
Type Function Line Description
1 test_functionArgument_varargin 4 Varargin contains zero elements; element 1 is requested.
2 test_functionArgument_varargin 5 Varargin contains zero elements; element 2 is requested.
3 test_functionArgument_varargin 7 Undef. function or variable 'number'. The first assignment to a local variable determines its class.
4 test_functionArgument_varargin 8 Undefined function or variable 'string'. The first assignment to a local variable determines its class.
5 test_functionArgument_varargin 5 varargin and varargout are not supported here.

채택된 답변

Walter Roberson
Walter Roberson 2025년 2월 16일
편집: Walter Roberson 2025년 2월 17일
C has no native way of expressing optional arguments. The convention that has grown up in C is to arrange all of the optional parts into a single **void pointer and bundle pointers to values in a vector, and put a null pointer at the end of the vector. The code would cast the individual *void pointers to the appropriate type for each element. It would be hypothetically possible for codegen to go through the trouble of implementing this convention, but it is a bit awkward.
C++'s native way of expressing option arguments is to define multiple functions with the same name but different function signatures, and then at compile time to select the function definition that matches the function signature that the code was actually called with. Generating such code automatically from MATLAB code that uses varargin would not be simple.
  댓글 수: 1
Bob Randall
Bob Randall 2025년 2월 20일
Hi Walter,
Thank you for the answer. I wish things were a little bit more akward. I was hoping that codegen would be a sofisticaded tool able to compile most of the matlab scripts we write with just some explicit type casting required. Unfortunately, I found myself writing matlab scripts or rewriting original matlab functions that resemble to C99 code just to be able to make it work with codegen. Even when codgen produces an executable, then it needs to be completely retested to ensure that behaves the same way as the original matlab script.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by