Best way to pass large amounts of variables between functions
50 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone! This is more of a general question rather than a straight single answer. What would be the best way to pass variables between functions? Such as: using a structure to store multiple variables, use a temporary .mat file containing the variables and load in when needed, use global variables, or pass many variables into each respective function call. I am redoing my codebase in order to try and save memory and increase its speed.
I ask because I have many initilization variables (30 - 40 unique variables) and output variables (20 unique variables each being NxNxM matricies N is 512 or 1024) and it is a nightmare for me to organize the information. I have many smaller functions compartmentalized but each function is dependent on the initilization variables.
If anyone has general hints or methods they would like to share for passing large amounts of information between functions let me know! Any insights would be greatly appriciated!
0 commentaires
Réponse acceptée
Stephen23
le 25 Août 2020
Modifié(e) : Stephen23
le 25 Août 2020
"What would be the best way to pass variables between functions?"
There is no simple answer because it depends on the nature of those variables and what you are doing with them.
Consider:
You could use a structure to pass many parameters, this is effective and easy to manage. But not everything should go in a structure, e.g. if you have some kind of optimization routine then the function parameterization values can go into one structure, but probably the optimization variables themselves should not.
Another very efficient and intuitive option is to use nested functions, which are very suitable for optimization routines or similar processes (I use them extensively for GUIs).
Avoid:
Do not use postional input/output arguments for more than six or seven or so arguments, more is unmanagable (this is my rule of thumb, no doubt other users have their own limits they feel comfortable with).
Using .mat files to pass data around should be avoided because the JIT engine has no chance to optimize the data handling within your code. It is also likely to slow down your code due to the OS/HW response times (although that depends significantly on the drive and HW you are using).
Global variables should be avoided: they are liable to bugs which are very difficult to debug.
My advice: trust the MATLAB parser. Trying to second-guess or micro-manage MATLAB's memory handling is unlikely to gain you much, and can easily end up being detrimental to overall efficiency. Stick to writing clear, understandable code and only optimize when you meet an actual bottle-neck in your code.
Search this forum to learn more about MATLAB's copy-on-write approach to passing data arrays:
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Variables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!