【问题标题】:MATLAB: How to pass data between 2 GUIs using the findobj-Function?MATLAB:如何使用 findobj-Function 在 2 个 GUI 之间传递数据?
【发布时间】:2015-12-10 09:42:07
【问题描述】:

我是使用 Matlab 创建 GUI 的新手。我有一个 MainGui,我从中打开一个 Subgui,以便用户可以单击复选框。单击确定按钮后,我的 Subgui 将关闭,并且再次看到 MainGui 表面。

我如何在不使用 getappdata 和 setappdata 的情况下访问 clickbutton 值,而 使用 findobj-function 来访问它,这对我来说更容易。

所以我在 MainGui 代码中寻找 Subgui

 hGui = findobj('Tag','Subgui');

其中“Subgui”是 SubGUI 的 Tag 属性的值。 手柄可见性对两者都启用!

    % get control handles for this GUI
    handlesSubgui = guidata(hGui);

    % now read the data from the checkbox
    checkValue = get(handlesSubgui.checkbox1,'Value');

为什么它不起作用?我设置了正确的标签并处理了可见性,但我得到了

hGui =

     Empty matrix: 0-by-1

!?

有人知道吗?我很乐意得到帮助! 最好的问候,约翰

【问题讨论】:

  • 这是 GUIDE GUI 还是程序化 GUI?
  • 它是用 GUIDE 创建的!它在多大程度上有所作为?
  • 如果hGui 在您按下按钮后关闭,我想MainGui 找不到它是有道理的?
  • 啊,好吧 Benoit_11,我想我总能找到图形对象,与状态无关,无论它们是否打开!谢谢,所以我将在使用 setappdata 和 getappdata 关闭窗口之前传递数据!谢谢,我真的很困惑!
  • 是的,我认为这是个好主意 :) 祝你好运!

标签: matlab user-interface checkbox handles


【解决方案1】:

在这种情况下要考虑的一个选项是在您的按钮回调中初始化一个小型 GUI。为了说明,我将设置一个小的程序化 GUI:

function testcode
res = get(0,'ScreenSize');
figdim = [300 300]; % Figure size, pixels

h.mainfig = figure( ...
    'Units', 'Pixels', ...
    'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
    'Name', 'This is the Main GUI', ...
    'Resize', 'off', ...
    'DockControls', 'off', ...
    'NumberTitle', 'off', ...
    'MenuBar', 'none', ...
    'Toolbar', 'none' ...
    );

h.subGUIbutton = uicontrol( ...
    'Parent', h.mainfig, ...
    'Units', 'Normalized', ...
    'Position', [0.25 0.6 0.5 0.3], ...
    'String', 'Open Checkbox GUI' ...
    );

h.displaydatabutton = uicontrol( ...
    'Parent', h.mainfig, ...
    'Units', 'Normalized', ...
    'Position', [0.25 0.1 0.5 0.3], ...
    'String', 'Display Checkbox Selections' ...
    );

% Requires R2014b or newer, otherwise we'll have to use set
try
    h.subGUIbutton.Callback = {@checkboxGUI, h};
    h.displaydatabutton.Callback = {@displaydata, h};
catch
    set(h.subGUIbutton, 'Callback', {@checkboxGUI, h});
    set(h.displaydatabutton, 'Callback', {@displaydata, h});
end

我们的回调结构如下:

function checkboxGUI(~, ~, handles)
res = get(0,'ScreenSize');
figdim = [200 200]; % Figure size, pixels
h2.mainfig = figure( ...
    'Units', 'Pixels', ...
    'Position', [(res(3) - figdim(1))/2 (res(4) - figdim(2))/2 figdim(1) figdim(2)], ...
    'Name', 'This is the Sub GUI', ...
    'Resize', 'off', ...
    'DockControls', 'off', ...
    'NumberTitle', 'off', ...
    'MenuBar', 'none', ...
    'Toolbar', 'none' ...
    );

% Build some checkboxes
for ii = 1:4
    h2.checkbox(ii) = uicontrol( ...
        'Parent', h2.mainfig, ...
        'Style', 'checkbox', ...
        'Units', 'Normalized', ...
        'Position', [0.25 (1 - ii*0.15) 0.5 0.1], ...
        'String', sprintf('Checkbox #%u', ii) ...
        );
end

h2.closebutton = uicontrol( ...
    'Parent', h2.mainfig, ...
    'Style', 'pushbutton', ...
    'Units', 'Normalized', ...
    'Position', [0.25 0.15 0.5 0.1], ...
    'String', 'Accept Changes', ...
    'Callback', {@closecheckbox} ...
    );

    function closecheckbox(~, ~)
        % requires R2014b or newer for dot notation
        try
            test = find([h2.checkbox(:).Value]); % Returns ID of checked boxes
        catch
            test = find(cell2mat(get(h2.checkbox(:), 'Value'))'); % Returns ID of checked boxes
        setappdata(handles.mainfig, 'BoxesChecked', test);
        close(h2.mainfig);
    end

waitfor(h2.mainfig); % Wait for user to close the checkbox GUI
end

function displaydata(~, ~, handles)
BoxesChecked = getappdata(handles.mainfig, 'BoxesChecked');

if isempty(BoxesChecked)
    fprintf('No boxes celected\n');
else
    fprintf('User selected box: %d\n', BoxesChecked);
end
end

请注意,为了便于阅读,我使用了nested function。在这个简单的例子中,我们的主 GUI 中有两个按钮,一个是打开用户提示的按钮,一个是显示按钮。当用户打开复选框提示时,所有 GUI 命令的执行都会暂停,直到提示关闭。单击显示按钮时,我们从应用数据中获取选中的值并将它们打印到命令窗口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多