How to formulate elegantly and performant functions that depend on a lot of input variables
Mostrar comentarios más antiguos
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 comentarios
Bjorn Gustavsson
el 23 de En. de 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
el 5 de Feb. de 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
el 5 de Feb. de 2021
To add to lk's comment, for releases prior to 20b check out input parser and other input validation techniques.
Stephen23
el 5 de Feb. de 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
el 5 de Feb. de 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
el 5 de Feb. de 2021
Another on of my favorites: The opposite order of inputs 1 & 2 in boxplot vs boxchart.
Walter Roberson
el 5 de Feb. de 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.
Respuesta aceptada
Más respuestas (3)
Divija Aleti
el 25 de En. de 2021
0 votos
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
el 1 de Feb. de 2021
0 votos
Usar variables globales podría ser una opción más eficiente que pasar una estructura
1 comentario
Walter Roberson
el 1 de Feb. de 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
el 2 de Feb. de 2021
0 votos
I usually using cell。
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!