简体   繁体   English

在Matlab GUIDE中保存GUI句柄

[英]Saving GUI handles in Matlab GUIDE

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)? 在Matlab GUIDE中,有什么方法可以保存GUI.m文件中的所有GUIhandle,这样我就可以在不同的函数中访问这些句柄(一个不同的.m文件,而不是GUI中的其他一个回调函数) .m文件)?

Note that I don't want to pass these handles manually to the other functions. 请注意,我不想手动将这些句柄传递给其他函数。

Use findall(figure_handle); 使用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');

Since you are designing your GUI using GUIDE, any uicontrol object you put on the the current figure (eg GUI.fig ) will have its handle added automatically to the handles structure, the variable that will be passed around between callbacks. 由于您使用GUIDE设计GUI,因此您在当前图形上放置的任何uicontrol对象(例如GUI.fig )都会自动将其句柄添加到handles结构中,该结构将在回调之间传递。 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. handles传统上也用于通过将这些变量添加到handles结构并使用guidata()函数保存handles来在回调之间传递任何其他程序变量。

The easiest and fastest way to pass handles to external functions is to send it as an input parameter to those functions. handles传递给外部函数的最简单,最快捷的方法是将其作为输入参数发送给这些函数。 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. 例如,如果您的其他外部文件名为auxiliary.m并且包含一个名为auxiliary(...)的函数,则只需设计auxiliary(...)以接受一个名为handles额外参数来接收所有图形句柄 - 以及任何其他手动添加变量。 This is exactly the way your GUI.m works right now. 这正是您的GUI.m现在GUI.m工作的方式。 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. 请注意, GUI.m看起来像一个单独的文件,但它实际上是许多回调函数的容器,其中每个函数都可以是一个单独的.m文件,其中包含一个同名的单个函数。 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. 例如,如果你削减pushbutton1_Callback(hObject, eventdata, handles)GUI.m然后将其粘贴在一个单独的pushbutton1_Callback.m文件,你的程序的工作原理完全相同的方式,只要有相同的重复的文件名称。

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: 如果仍然坚持不将句柄直接传递给外部函数,只需保存handles结构并将其加载到第二个.m文件中:

% 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. 我推荐第一种方法,因为没有IO开销,你不需要数据持久性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM