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

1 visualización (últimos 30 días)
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
Adam Danz
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
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.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 23 de En. de 2021
Editada: Adam Danz el 23 de En. de 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.

Más respuestas (3)

Divija Aleti
Divija Aleti el 25 de En. de 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 comentarios
Adam Danz
Adam Danz el 25 de En. de 2021
Editada: Adam Danz el 25 de En. de 2021
The OP hasn't indicated that they want to name variables dynamically, though.
Ferdinand Breit
Ferdinand Breit el 26 de En. de 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!

Iniciar sesión para comentar.


David Correa
David Correa el 1 de Feb. de 2021
Usar variables globales podría ser una opción más eficiente que pasar una estructura
  1 comentario
Walter Roberson
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.

Iniciar sesión para comentar.


MATLAB WAN
MATLAB WAN el 2 de Feb. de 2021
I usually using cell。

Categorías

Más información sobre Variables en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by