Specify Number of Input or Output Arguments to Entry-Point Functions - MATLAB & Simulink (original) (raw)

Main Content

If your MATLAB® code takes more inputs or produces more outputs than you want to appear in the generated code, you can instruct the code generator not to generate code for the unneeded arguments.

Control Number of Input Arguments

You can control the number of input arguments in the generated entry-point function by using one of these approaches:

Consider these functions, both of which support a flexible number of input arguments:

function [x,y] = myops1(varargin) %#codegen if (nargin > 1) x = varargin{1}+varargin{2}; y = varargin{1}*varargin{2}; else x = varargin{1}; y = -varargin{1}; end

function [x,y] = myops2(a,b) %#codegen if (nargin > 1) x = a+b; y = a*b; else x = a; y = -a; end

At the Command Line

At the command line, generate a function that takes only one input argument by providing one argument to the -args option of the codegen command. For example:

codegen myops1 -config:lib -args {0}

codegen myops2 -config:lib -args {0}

The generated function signatures include only one input argument.

void myops1(double varargin_1, double *x, double *y)
void myops2(double a, double *x, double *y)

Using the MATLAB Coder App

To generate a function that takes only one argument by using the MATLAB Coder app, add myops1 as an entry-point function. In theEntry Points pane, the app shows that the input tomyops1 is varargin.

Entry Points pane, showing undefined varargin

To add an argument, click the Add Input button .

Entry Points pane, showing definition of varargin{1}

Control Number of Output Arguments

You can control the number of output arguments for the generated entry-point function by using the codegen function at the command line or by using the MATLAB Coder app.

Consider these functions:

function varargout = myops3(a,b) %#codegen varargout{1} = a+b; varargout{2} = a*b; varargout{3} = a/b; varargout{4} = a-b; end

function [output1,output2,output3,output4] = myops4(a,b) %#codegen output1 = a+b; output2 = a*b; output3 = a/b; output4 = a-b; end

At the Command Line

At the command line, generate a function that outputs only three arguments by using the -nargout option with the codegen function.

codegen myops3 -config:lib -args {0,0} -nargout 3

codegen myops4 -config:lib -args {0,0} -nargout 3

The generated function signatures include only three output arguments.

void myops3(double a, double b, double *varargout_1, double *varargout_2,
            double *varargout_3)
void myops4(double a, double b, double *output1, double *output2,
            double *output3)

Using the MATLAB Coder App

When you use the MATLAB Coder app, you control the number of output arguments differently depending on whether or not the function uses varargout.

If the MATLAB function uses vargout, the app assumes that the function has one output argument. In the Entry Points pane, the app displays vargout{1} in the function signature. To change the number of outputs, click the varargout argument and enter the desired number of outputs.

Entry Points pane, showing function myops3 with 3 outputs

If your MATLAB function does not use vargout, the app displays the output arguments in the function signature in the Entry Points pane. To disable one or more output arguments, click an output argument. The app disables the output argument you click and all arguments to the right. The app shows disabled outputs in a lighter color with a strike-though line.

Entry Points pane, showing function myops4 with 3 outputs

See Also

MATLAB Coder | codegen | varargin | varargout

Topics