Efficient methods for MATLAB code
2 views (last 30 days)
Show older comments
Hi everyone,
i'm quite new to MATLAB and also programming as such.
I'm currently refactoring an old Software. Often when i'm creating new functions i need a lot of variables as Input. I guess some of them could be stored in the handles structure that is constantly being passed around.
So now i'm wondering what is more efficient, respectivley less time comsuming a) Storing variables in the handle structure(making it larger and maybe heavier to handle for MATLAB) b) Creating functions that have a lot of Input variables
I'm just curious and it would be great if someone could answer this.
Best regards Julian
0 Comments
Accepted Answer
Matt J
on 30 Aug 2018
Both are equivalent as far as consuming computational resources is concerned. There is no fundamental difference between structure fields and individual variables. From Matlab's point of view, it's just a different way of naming them.
2 Comments
Matt J
on 30 Aug 2018
Edited: Matt J
on 30 Aug 2018
If you have lots of related variables that often need to travel together, it makes sense to store them in a structure and pass them around that way. That's what structures are for.
In terms of speed and computational burden, there is no big difference between passing 1 variable or a million variables. Matlab isn't making copies of these variables every time you pass them to a function, if that's what you're worried about. They all get passed with pointers. See, for example,
More Answers (0)
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!