Best practice to pass large number of parameters to many functions?
Mostra commenti meno recenti
This a programming style question. I have to solve a model for given parameters. The parameters are fixed in the beginning and they do not change during execution. These parameters need to be available to all the functions and subfunctions of the code. So far I used global variables. This is not very elegant, but it worked. But when I tried to speed up my code with using "parfor" instead of "for", then I learned that globals and parfor don't play well together.
So now I am planning to pass all the parameters to all functions as input arguments. Obviously, I don't want to write out each of them and create functions with 20-30 inputs. What is the best practice to do this? Should I use a structure?
Thanks, Andras
6 Commenti
Andras
il 1 Ago 2014
David Russell
il 10 Apr 2018
This thread is several years old, but I have a similar question. I was using structs/objects with parameters as fields/properties, but I'm now thinking of switching to saving a whole workspace instead, for a few reasons, and I'm wondering if there is a significant disadvantage that I'm overlooking. My reasons are:
1. I want to save the parameters (as well as initial conditions and other options) to file anyway to preserve for postprocessing of model trajectories. So if I used a struct, I'd have to save the struct.
2. I have a script where the user sets the parameters, and it seems simpler to be able to enter something like
sigma = 1;
rather than
params.sigma = 1;
especially when there are so many parameters to track.
Adam
il 11 Apr 2018
Well, File I/O is likely to be slower than argument passing, which may or may not be an issue, especially if your workspace contains a lot of other stuff that you don't need. I assume this also means you'd have to pass the filename in as an argument to the function.
It works, there are just very few advantages to it in general.
John D'Errico
il 11 Apr 2018
Loading an entire file will generally be extremely slow compared to passing arguments around. Even if disk access is to a solid state drive, the bus speeds for a file load will be slow compared to main memory access. And to load a .mat file forces MATLAB to read and unpack the file format into memory. Avoid putting loads of .mat files into functions, if that will ever be done more than once.
If you get tired typing the variable name params, then just use p. So p.sigma costs two extra characters. If the only cost is your inconvenience of typing p.sigma, rather than a simple sigma, is that really that hard?
If you will need to use a variable a lot, then in the beginning of your function, do this once:
sigma = p.sigma;
Hari Haran
il 4 Ott 2018
Modificato: Hari Haran
il 4 Ott 2018
Adam
il 5 Ott 2018
You would be better off opening a new thread to ask a question. I assume you are just pressing Run on your program though, in which case obviously it will not have any input arguments, which it appears to expect so you will get that error as soon as it reaches a line where one of those input arguments will be used.
Risposta accettata
Più risposte (2)
Kelly Kearney
il 1 Ago 2014
5 voti
I use structures for this sort of thing. I also usually set up functions so that they can handle input as either structure or parameter/value pairs. When appropriate, the functions will also be coded to fall back on specific default values, so I don't always have to provide all the parameters.
4 Commenti
Andras
il 1 Ago 2014
Kelly Kearney
il 1 Ago 2014
For more complex functions that require a lot of inputs, I usually use inputParser objects. They're a bit of overkill for simple functions (and I usually use a much simpler parser for those), but for something like this they're great.
As far as getting the defaults to all the subfunctions, I would set up the input parser at the beginning of your main function, where it would fill in any missing values with defaults. Store all the resulting parameters in a structure and then pass that to the subfunctions.
For example, here is an example of an input-parsing function that I use with one of the models underlying my research. It's called at the very start of a simulation, and it checks the many input variables for format, fills in defaults, and collects everything together.
Andras
il 1 Ago 2014
Ajay
il 10 Nov 2022
hi can u share the example model domain has expired
Adam
il 1 Ago 2014
2 voti
Nowadays I generally use classes with a regular setup of a class (or many collaborating, but one front end at least) to run the actual algorithm(s) or solve the model or whatever it is and an arguments/parameters class that I pass in.
The parameters class is always very simple, effectively just a struct, but with the added bonus that I use validateattributes in the set functions of all the parameters to ensure they are in the correct format before they get anywhere near the algorithm.
Using classes with parallel programming does require a bit of care though as I have made many mistakes with it!!
Categorie
Scopri di più su Whos in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!