Can I declare global handles and/or variables?

12 views (last 30 days)
Can I declare global handles? Variables?

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 6 Jun 2011
Yes, you can. But really, really try to avoid it. See for example: http://matlab.wikia.com/wiki/FAQ, or search for discussions on the matlab newsgroup. In short globals make code tricky to debug because there is only one global namespace - meaning if you happen to have 2 groups of functions that happens to use the same variable name there will be one variable that will be set by functions from both groups _and_then_used == errors will be caused by functions that are completely unrelated to where it blows up.
A presonal note: I included rather rapidly a few globals in a number of functions in a toolbox I wrote ~15 years ago - it was a poor design choise, but I still haven't completely weeded them out.
So sit back, take a second glance at your problem and draw up a good clean design. I've heard people use globals for very large matrices, but then others have suggested class-definitions and other tricks, if this is your problem you could also look at memmapfile.

More Answers (1)

Jan
Jan on 6 Jun 2011
Yes. Simply add this line to each function, which uses the global variables "handles":
global handles
The fact, that you've posted 3 threads concerning "handles" seems, like you are confused about this topic. It would be more efficient, if you explain, what you are trying to achieve.

Categories

Find more on Performance and Memory 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!