GUIs MATLAB的句柄结构 [英] Structure of handles of GUIs MATLAB

查看:148
本文介绍了GUIs MATLAB的句柄结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有三个GUI.它们每个的标签是'P0''P1''P2'.我想将所有三个GUI的句柄放在一个结构中,并能够从三个GUI中的任何一个进入此结构并更新其中的值.最好的方法是什么?

I have three GUIs in MATLAB. The tag of each of them is 'P0', 'P1' and 'P2'. I would like to put the handles of all three GUIs in a structure and be able to get to this structure from any of three GUIs and update the values in it. What is the best way to accomplish this?

推荐答案

关于如何执行此操作,您有两个选择.一种方法是使用根图形对象以及setappdatagetappdata来存储和检索值.

You have a couple of options on how to do this. One way would be to use the root graphics object along with setappdata and getappdata to store and retrieve values.

fig0 = findall(0, 'tag', 'P0');
fig1 = findall(0, 'tag', 'P1');
fig2 = findall(0, 'tag', 'P2');

% Combine the GUIdata into a single struct
handles.P0 = guidata(fig0);
handles.P1 = guidata(fig1);
handles.P2 = guidata(fig2);

% Store this struct in the root object where ALL GUIs can access it
setappdata(0, 'myappdata', handles);

然后从您的回调中,只需获取此结构并直接使用

Then from within your callback, you'd simply fetch this struct and use it directly

function mycallback(hObject, evnt, ~)
    % Ignore the handles that is passed in and use your own
    handles = getappdata(0, 'myappdata');

    % Now if you modify it, you MUST save it again
    handles.P0.value = 1;

    setappdata(0, 'myappdata', handles)
end

另一种选择是使用 handle来存储您的值,然后您可以在每个GUI的handles结构内存储对该句柄类的引用.当您对此结构进行更改时,更改将反映在所有GUI中.

Another option is to use a handle class to store your values and then you can store a reference to this handle class within the handles struct of each GUI. When you make changes to this struct, the changes will be reflected in all GUIs.

一种简单的方法是使用 structobj (免责声明:我是开发人员),它将任何struct转换为handle对象.

An easy way to do this would be to use structobj (Disclaimer: I am the developer) which will convert any struct into a handle object.

% Create an object that looks like a struct but is a handle class and fill it with the 
% handles struct from each GUI
handles = structobj(guidata(fig0));
update(handles, guidata(fig1));
update(handles, guidata(fig2));

% Now store this in the guidata of each figure
guidata([fig0, fig1, fig2], handles)

由于我们在图的guidata中存储了一个东西,它将通过标准的handles输入参数自动传递到您的回调中.因此,您的回调现在看起来像:

Since we stored a thing within the guidata of the figure, it will automatically be passed to your callback via the standard handles input argument. So now your callback would look something like:

function mycallback(hObject, evnt, handles)
    % Access the data you had stored
    old_thing = handles.your_thing;

    % Update the value (changes will propagate across ALL GUIs)
    handles.your_thing = 2;
end

这种方法的好处是您可以同时运行三个GUI的多个实例,并且数据不会互相干扰.

The benefit of this approach is that you can have multiple instances of your three GUIs running at the same time and the data will not interfere with each other.

这篇关于GUIs MATLAB的句柄结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆