When you use varargin
and varargout
for
code generation, adhere to these restrictions:
You cannot use varargout
in the
function definition for a top-level function.
You cannot use varargin
in the
function definition for a top-level function in a MATLAB Function block
in a Simulink® model, or in a MATLAB® function in a Stateflow® diagram.
If you use varargin
to define
an argument to a top-level function, the code generator produces the
function with a fixed number of arguments. This fixed number of arguments
is based on the number of example arguments that you provide on the
command line or in a MATLAB Coder™ project test file.
A top-level function is:
For example, the following code generates compilation errors:
%#codegen
function varargout = inch_2_cm(varargin)
for i = 1:length(varargin)
varargout{i} = varargin{i} * 2.54;
end
To fix the problem, write a top-level function that specifies
a fixed number of inputs and outputs. Then call inch_2_cm
as
an external function or local function, as in this example:
%#codegen
function [cmL, cmW, cmH] = conv_2_metric(inL, inW, inH)
[cmL, cmW, cmH] = inch_2_cm(inL, inW, inH);
function varargout = inch_2_cm(varargin)
for i = 1:length(varargin)
varargout{i} = varargin{i} * 2.54;
end