0

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.

Ananth Saran
  • 207
  • 2
  • 17

2 Answers2

1

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');
Hugh Nolan
  • 2,508
  • 13
  • 15
  • Thanks! But that means that I still have to pass uihandles, doesn't it? I don't want to do that also. Is that possible? – Ananth Saran Jun 17 '13 at 13:07
  • 3
    What @Hugh means is that if you know the figure handle ahead of time (or if you expect the figure to be the active figure, i.e. `gcf`) then from your second `.m` file you can use `findall` to dynamically retrieve all the `uicontrol` handles from that figure. His code is the code you need to add in the external function, not GUI.m. – Bee Jun 17 '13 at 13:30
  • You can also find the figure handle by setting a tag in the generation function - set(fig_handle,'Tag','MyUniqueTag') - and using fig_handle=findall(0,'Tag','MyUniqueTag') – Hugh Nolan Jun 17 '13 at 15:22
1

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.

Bee
  • 2,472
  • 20
  • 26
  • :Thanks!!!Exactly what I needed. There is a specific reason as to why I didn't want to pass handles, its because the function which needed the handles was buried deep inside another set of functions in the first place. Didn't want to pass handles through so many functions. – Ananth Saran Jun 17 '13 at 13:38
  • 1
    Just some info on an alternative: a useful set of functions in this case is "assignin" and "evalin". Using: "assignin('base','temphandles',handles);" from inside any function will assign "handles" from that function into "temphandles" in the base workspace. Conversely, using "handles = evalin('base','temphandles');" will assign "temphandles" from the base workspace into "handles" in the current function. – Hugh Nolan Jun 17 '13 at 15:19