How to formulate elegantly and performant functions that depend on a lot of input variables
Show older comments
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
Bjorn Gustavsson
on 23 Jan 2021
Sometimes a function requires a large number of input arguments. Then it is just to accept ones fate and program it that way. When it comes to options-type arguments I often combine them into a single struct. I find that neater than the "name-value" system used in matlab. But that is partly another question, and a matter of taste.
A large number of input arguments increases the risk of mixing up the inputs when calling the function. I have answered questions on this forum where bugs were caused by exactly this problem: too many input arguments, changes to the function, OP lost track of the inputs, no warnings or errors, surprisingly difficult to track down. How descriptive the names are is irrelevant for this to occur.
Use a structure. Acessing data from the structure is unlikely to be the slowest part of your code.
If after billions/trillions of iterations your code runs five minutes slower accessing data from a structure but you save yourself hours of debugging time, then it is still a net gain for you. Summary: write code that is reliable, this is a better use of your time.
lk
on 5 Feb 2021
To mitigate the problem that Stephen Cobeldick mentions, namely
Stephen Cobeldick on 24 Jan 2021 at 6:35
... OP lost track of the inputs, no warnings or errors, surprisingly difficult to track down. ...
You could try (if you haven’t already) function argument validation introduced in R2020b. In particular, it will provide specific error messages for invalid inputs.
Here is the doc link
https://www.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html
Adam Danz
on 5 Feb 2021
To add to lk's comment, for releases prior to 20b check out input parser and other input validation techniques.
Stephen23
on 5 Feb 2021
@lk: that works if the data are distinguishable. A whole lot of scalar numeric parameters may be completely indistiguishable by value or range if swapped around... you can find threads on this forum where this has occurred.
Bjorn Gustavsson
on 5 Feb 2021
@Stephen: In the case of "forgetting order" one has the help-section. If my head is really on the line I make sure to check even the order of inputs to atan2.
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
More Answers (3)
Divija Aleti
on 25 Jan 2021
0 votes
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
David Correa
on 1 Feb 2021
0 votes
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.
MATLAB WAN
on 2 Feb 2021
0 votes
I usually using cell。
Categories
Find more on Cell Arrays 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!