MATLAB Answers

How to formulate elegantly and performant functions that depend on a lot of input variables

4 views (last 30 days)
Hello,
I am not new to MATLAB. However, I have the following problem:
If I have a function e.g.:
output = f(in1,in2,in3,in4,in5,in6,...)
which depends on a lot of input variables.
How can I design it as elegant as possible but at the same time performant?
If I use a struct e.g.
input.in1
input.in2
input.in3
...
I can formulate the function nice and short:
output = f(input)
But the problem is that the variable naming is fixed for the input as well as in the function. Furthermore, calling values from a struct is slow, so in the function they have to be read first:
function [output] = f(input)
in1 = input.in1
in2 = input.in2
in3 = input.in3
...
% calculations with in1,in2,in3,...
end
I also know that you should not write a function with so many input variables, yet it happens.
How do you handle such cases?
  8 Comments
Walter Roberson
Walter Roberson on 5 Feb 2021
But the problem is that the variable naming is fixed for the input as well as in the function.
Though there is a sense in which name/value pairs involves "variable naming is fixed for the input as well as in the function", and people use name/value pairs all the time.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Jan 2021
Edited: Adam Danz on 23 Jan 2021
A set of inputs like the example you shared is not inelegent. Some may argue that it's more elegant than the alternatives. What is inelegent is the undescriptive function and variable names which I'm guessing is just for the example.
  • If all inputs are numeric scalars or arrays of the same size and class, you could require the user to concatentate the values into a single multidimensional array and then the function can index or deconstruct the array as needed, but that's more work if the inputs are typically indpendent variables to begin with. Curve fitting functions often require this syntax for the objective function.
  • Another common solution is to use a cell array that support mixed classes and sizes but that also requires more work for the user to set up. For example,
in = {1, struct('t',5), 'a', false};
out = f(in);
  • If some of the inputs are not required you could use a varargin to reduce the number of input variables declared within the function. That requires carefully unpacking them, of course.
  • If the goal is to keep the variable references tidy within the function, you can use the input parser which validates and stores all inputs within a structure inside the function. That structure is used instide the function instead of the input variable names. I often use this approach to tidy-up the code.

More Answers (3)

Divija Aleti
Divija Aleti on 25 Jan 2021
Hi Ferdinand,
Have a look at the Accepted Answer (and the comment under it) for the question posed in the following link:
For more information, refer to the link given below:
Regards,
Divija
  2 Comments
Ferdinand Breit
Ferdinand Breit on 26 Jan 2021
Hello,
thanks for your answers and comments, then it seems there is no real solution without using strcuts or cells.
Knowing this is also very helpful. Thanks!

Sign in to comment.


David Correa
David Correa on 1 Feb 2021
Usar variables globales podría ser una opción más eficiente que pasar una estructura
  1 Comment
Walter Roberson
Walter Roberson on 1 Feb 2021
No, global variables are the slowest kind of variables that exist. Referencing a variable that is a parameter is the fastest kind of variable access.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by