Question

In Matlab GUIDE, is there any way in which I can save all my GUIhandles from a GUI.m file so that I can access these handles in a different function (a different .m file altogether, not one of the other callbacks in the GUI.m file)?

Note that I don't want to pass these handles manually to the other functions.

Was it helpful?

Solution

Since you are designing your GUI using GUIDE, any uicontrol object you put on the the current figure (e.g. GUI.fig) will have its handle added automatically to the handles structure, the variable that will be passed around between callbacks. handles is also traditionally used to pass any other program variables between callbacks by adding those variables to the handles structure and saving handles using guidata() function.

The easiest and fastest way to pass handles to external functions is to send it as an input parameter to those functions. For example, if your other external file is called auxiliary.m and contains a function called auxiliary(...), just design auxiliary(...) to accept one extra parameter called handles to receive all figure handles -- plus any other manually added variables. This is exactly the way your GUI.m works right now. Note that GUI.m looks like a single file but it's really a container for a lot of callback functions, where each one of them could be a separate .m file containing a single function of the same name. For example, if you were to cut pushbutton1_Callback(hObject, eventdata, handles) out of GUI.m and paste it in a separate pushbutton1_Callback.m file, your program works the exact same way as long as there is no duplicate files of the same name.

If you still insist on not passing the handles directly to the external function, just save the handles structure and load it in the second .m file:

% inside GUI.m
save('handles.mat', 'handles');

%inside auxiliary.m
load('handles.mat', 'handles');

I recommend the first method since there is no IO overhead and you don't need data persistence.

OTHER TIPS

Use findall(figure_handle);

Example:

F=figure;
H=uicontrol('parent',F,'style','pushbutton');
uihandles=findall(F,'type','uicontrol');

If you don't have the figure handle directly, you can use

uihandles=findall(gcf,'type','uicontrol');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top