MATLAB GUI callbacks and function variable scope

160 views Asked by At

I think I have a basic understanding of scope, but I am a bit confused as to how this would work in a MATLAB GUI.

For instance, if I had a GUI that did a surf plot on an axes based on input from editBox1, I would have:

An updateAxes function that would update the axes with input from editBox1 (str2double(get(handles.editBox1, 'String'))).

An editBox1 callback that would call the updateAxes function.

Does it violate the idea of scope for updateAxes to directly use the get function? Should I be passing in the content of editBox1 as a parameter to updateAxes?

1

There are 1 answers

0
patrik On BEST ANSWER

It is good to think about what you really need and not pass all data around. Still, handles is, in my opinion, something that can be passed around. You should also think that even if the handles are not passed around, you still have access to them. Figures are in essence global, since any figure (and thus figure properties as well) can be accessed through any function. By not passing handles around you can make it harder to access the figures, but cannot prevent access. If you have in mind to modify a figure, I can see no reason not to pass the handle to figure, or the handles to all objects in the figure. The second option will spare you some headache having to search the (recursive) list of Children in case you find out that you would need another handle to the same figure. As stated in a comment the GUI matlab have a simple way of handling this through the function guidata. Also, thinking about maintainability. Would it be easier to maintain code with a selected set of handles, properly named or sorted, or would you rather have to access them through a nameless set of Children?