MATLAB: Is there a way to integrate external C code with variable vector as output using Legacy code tool

simulink

I want to integrate external C code with Simulink using the Legacy Code Tool. The output array size from C function should be variable, and need to be specified in Simulink, but not in the Legacy Code Tool. Unfortunately, it will throw an error if the array size of the outpus is undefined:
Error using legacycode.LCT.legacyCodeImpl
An output or a work dimension must be fully specified:
–> myCfun(single u1[],uint16 u2, single u3, single u4, single u5[], single y1[])
Is there a way to integrate external C code with variable vector as output using Legacy code tool?

Best Answer

Please refer to the section "*Legacy Code Tool Function Specification Rules*" in the documentation:
"You can specify argument dimensions with expressions that use the following:
  • Functions: numel , size
  • Parameter values
  • Operators: +, -, *, and /
  • Integer and floating point literals
  • Parentheses for grouping sub-expressions "
In R2019b, it works with:
def.OutputFcnSpec = 'myCfun(single u1[],uint16 u2, single u3, single u4, single u5[], single y1[numel(u1)])'
In a release older than R2016b you can use 'single y1[size(u1,1)]' instead of 'numel':
def.OutputFcnSpec = 'myCfun(single u1[],uint16 u2, single u3, single u4, single u5[], *single y1[size(u1,1)]*)'