Info
This question is closed. Reopen it to edit or answer.
Doubt on how to simplify inputs in functions.
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
Cedric
on 27 Jan 2013
Edited: Cedric
on 27 Jan 2013
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 Comments
Cedric
on 27 Jan 2013
Edited: Cedric
on 27 Jan 2013
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.
Cedric
on 27 Jan 2013
Edited: Cedric
on 27 Jan 2013
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.
Matt J
on 27 Jan 2013
Edited: Matt J
on 27 Jan 2013
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
on 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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!