How to formulate elegantly and performant functions that depend on a lot of input variables
1 view (last 30 days)
Show older comments
Ferdinand Breit
on 19 Jan 2021
Commented: Walter Roberson
on 5 Feb 2021
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
Adam Danz
on 5 Feb 2021
Another on of my favorites: The opposite order of inputs 1 & 2 in boxplot vs boxchart.
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.
Accepted Answer
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.
0 Comments
More Answers (3)
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
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
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.
See Also
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!