Doubt on how to simplify inputs in functions.
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
Hello,
Let's say I have a function that is called by many other functions in my problem.
function y = myfuncA(A,x)
For exemple, A is a matrix and x an integer.
If I update that function, myfuncA, and add a new input, a value, so it would become this:
function y = myfuncA(A,x,n)
Now I must update the calling of that function in many different files from other functions, to add that new argument, n.
Is there a way where I can group all my inputs, so I won't need to search for all the callings on that function, and update only the definition of this new group? Considering that some arguments might be doubles, integers, matrix, strings, and so on all mixed.
Something like:
input_A = {A, x}
then I would call:
myfuncA(input_A)
And then, updating only input_A with the new argument it would be enough
input_A = {A, x, n}
Thanks for any help.
Réponses (3)
You can always use varargin
function y = myFuncA(A, x, varargin)
if isempty(varargin)
b = 0 ;
else
b = varargin{1} ;
end
y = A * x + b ;
end
EDIT: with this, you would avoid having to group your parameters for each call.
5 commentaires
Matt J
le 27 Jan 2013
In fact, even varargin may not be necessary. NARGIN might be enough
function y = myFuncA(A, x, n)
if nargin<3,
n=0;
end
y = A * x + n ;
end
Focu
le 27 Jan 2013
Cedric
le 27 Jan 2013
Yep, right, bad old habit of mine to jump directly on varargs solutions!
Well, using nargin or varargin solutions make the interface flexible. If tomorrow you decide that your function could take 3 more args, you can just set them to default values when they are not defined during the call and use them otherwise. This way you don't have to modify former calls, and you can make new calls that use these extra args if needed.
The function defined in my answer, or better in Matt's comment, can be called with
y = myFuncA(rand(2), 3) ; % Former calls; they are still valid
% (b or n is set to default=0 internally).
or
y = myFuncA(rand(2), 3, 5) ; % 'New' call exploiting the new arg for
% defining n or b.
If you had called your function like this
myfuncA(input_A{:})
then it would only have been necessary to update input_A={A,x,n} in one location.
Walter Roberson
le 27 Jan 2013
Yes, you can supply a cell array as the argument to a function, provided that the function pulls apart the cell array as needed.
Alternately, you can code,
input_A = {A, x, n};
myfuncA(input_A{:})
which will pull apart the cell array into individual arguments.
Either way you are going to have to change your calling sequence everywhere each time you want to add another argument.
You might also want to consider passing in structures, where the structure field names indicate the purpose of each argument. After you make that initial adjustement, you would then only have to update the calls in places where you want to add parameters, or in places where a parameter has become mandatory (possibly only mandatory in conjunction with another paramter).
In situations where you are adding a new parameter to the calling sequence but it is not always needed, with the necessity being determined by the values of the existing parameters, or with the behavior triggered by the new parameter not usually being the desired behavior, then you can use variable number of arguments by using varargin.
The "MATLAB way" of handling varying arguments is to use property/value pairs.
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!