MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Doubt on how to simplify inputs in functions.

Asked by Focu on 27 Jan 2013

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.

## Products

No products are associated with this question.

Answer by Cedric Wannaz on 27 Jan 2013
Edited by Cedric Wannaz 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.

Cedric Wannaz on 27 Jan 2013

Yep, right, bad old habit of mine to jump directly on varargs solutions!

Cedric Wannaz 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 Wannaz 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.
Answer by Matt J on 27 Jan 2013
Edited by Matt J on 27 Jan 2013

myfuncA(input_A{:})

then it would only have been necessary to update input_A={A,x,n} in one location.

Focu on 27 Jan 2013

The problem is if in one case, I`m in a loop, where variable x is changing and myfuncA is being called. I must redefine input_A={A,x,n} all the time with the values of A and n as well.

Matt J on 27 Jan 2013

I'm not sure why you think so. Why not just update x in the loop like so

input_A={A,x,n};
for i=1:N
input_A{2}= x_new;
myfuncA(input_A{:});
end
Focu on 27 Jan 2013

Yes, but my initial problem it would be the same. I would need to open a lot of files that are calling myfuncA, look for each call and insert a line updating input_A. It would be similar to find that function calling and add the n, like myfuncA(A,x,n).

What I wanted initially was to find a way that updating some feature to that function where I must add some argument input (in that case n), I wouldn't need to find hundreds of lines calling myfuncA and changing it from myfuncA(A,x) to myfuncA(A,x,n).

Answer by 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.