How do I write a function that behaves differently according to the number of input arguments?

조회 수: 4 (최근 30일)
First, is it possible to write a function that variably accepts some number of arguments? (It should be possible since to create objects, you often call functions and specify a variable number of properties that you wish to initialize).
Secondly, how do you count the number of arguments? Are arguments received as strings?

답변 (2개)

Steven Lord
Steven Lord 2015년 9월 10일
Take a look at VARARGIN, NARGIN, and NARGINCHK.

Thorsten
Thorsten 2015년 9월 10일
편집: Thorsten 2015년 9월 10일
Use varargin as the parameter of your function that collects all arguments as elements of a cell array.
function myplot(varargin)
%number of arguments
numel(varargin)
% first argument
varargin{1}
You can use other arguments in front of varargin.
function myplot(x, y, varargin)

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by