使用MATLAB GUI中另一个图中的一个图形变量

时间:2014-04-30 20:49:48

标签: matlab matlab-guide

我有一个使用GUI Layout Toolbox创建的MATLAB GUI。这个GUI有一个主要人物。主要人物有一个按钮,可以调用辅助人物。有一个通过主要图形调用的函数,它从次要图形访问一些变量。在代码的开头或者直到第二个数字被打开的点,一切正常。打开辅助节点后,如果我将其打开,则函数调用正常,但如果我关闭辅助数字,则函数调用将停止工作。

下面是我如何定义变量和函数调用的片段:

S.Fig1 = figure();
S.var1 = uicontrol('parent',S.Fig1,...
                      'style','edit');
S.Fig2 = figure();
S.var2 = uicontrol('parent',S.Fig2,...
                      'style','edit');

S.var1与函数调用var1_call()相关联,并且在该函数内部我正在检查S.var2的值。 如果辅助数字打开,则正确提供值,否则语句将显示错误,说明"无效的句柄对象"

如果我不能像我一样定义这两个数字,请告诉我。如果可以,我怎么能在打开它后检查fig2是否关闭。

谢谢

添加以下示例代码: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%% %测试脚本以检查从主图中调用新图 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%

function [] = TestCallingNewWindow()

SCR = get(0,'Screensize');  % Get screensize.

% Open Figure
S.fh = figure('numbertitle','off',...
              'menubar','none',...
              'units','pixels',...
              'position',[SCR(3)/2-800 ,SCR(4)/2-100 , 500, 200],...
              'name','TestCallingWindow',...
              'resize','off');

% Create PushButtons
S.pb1 = uicontrol('style','pushbutton',...
                  'units','pixels',...
                  'position',[20 120 200 30],...
                  'string','Open New Window',...
                  'fontsize',12);

for i=1:6
    S.Select(i) = uicontrol('parent',S.fh,...
                      'style','checkbox',...
                      'units','pixels',...
                      'position',[250 (165-((i-1)*30)) 30 20],...
                      'string',sprintf('%d',i),...
                      'enable','on',...
                      'fontsize',10);
    S.Type(i) = uicontrol('parent',S.fh,...
                      'style','text',...
                      'units','pixels',...
                      'position',[300 (165-((i-1)*30)) 60 20],...
                      'string','Data',...
                      'enable','on',...
                      'fontsize',10);
    S.TypeVal(i) = uicontrol('parent',S.fh,...
                      'style','edit',...
                      'units','pixels',...
                      'position',[365 (165-((i-1)*30)) 80 20],...
                      'string','0',...
                      'enable','on',...
                      'fontsize',10);
end

% Create the Pop-up Figure
S.popfh = figure('numbertitle','off',...
              'menubar','none',...
              'units','pixels',...
              'position',[SCR(3)/2-200 ,SCR(4)/2-100 , 300, 200],...
              'name','Pop-Up Window',...
              'resize','off',...
              'visible','off');

for i=1:6
    S.popSelect(i) = uicontrol('parent',S.popfh,...
                      'style','checkbox',...
                      'units','pixels',...
                      'position',[50 (165-((i-1)*30)) 30 20],...
                      'string',sprintf('%d',i),...
                      'enable','on',...
                      'fontsize',10);
    S.popType(i) = uicontrol('parent',S.popfh,...
                      'style','text',...
                      'units','pixels',...
                      'position',[100 (165-((i-1)*30)) 60 20],...
                      'string','Data',...
                      'enable','on',...
                      'fontsize',10);
    S.popTypeVal(i) = uicontrol('parent',S.popfh,...
                      'style','edit',...
                      'units','pixels',...
                      'position',[165 (165-((i-1)*30)) 80 20],...
                      'string','0',...
                      'enable','on',...
                      'fontsize',10);
end

% Set callback functions
set(S.Select(:),'callback',{@main_call,S})
set(S.TypeVal(:),'callback',{@main_call,S})
set(S.pb1,'callback',{@pb1_call,S}) 
set(S.popSelect(:),'callback',{@pb1_call,S})
set(S.popTypeVal(:),'callback',{@pb1_call,S})

% Function Definitions
function [] = main_call(varargin)
    [h,S] = varargin{[1,3]};  % Get calling handle and structure.
    popWin = findobj('type','figure','name','Pop-Up Window');
    for idx = 1:6
        if(~isempty(popWin))
            popenable = get(S.popSelect(idx),'Value');
        else
            popenable = 0;
        end
        if(popenable == 0)
            enable = get(S.Select(idx),'Value');
            if(enable == 1)
                data = str2double(get(S.TypeVal(idx),'String'));
                if(~isempty(popWin))
                    set(S.popTypeVal(idx),'string',data);
                end
            end
        else
            data = str2double(get(S.popTypeVal(idx),'String'));
        end
    end
end

% po-up window
function [] = pb1_call(varargin)
    [h,S] = varargin{[1,3]};  % Get calling handle and structure.

    set(S.popfh,{'visible'},{'on'});
    for idx = 1:6
        popenable = get(S.popSelect(idx),'Value');
        if(popenable == 0)
            enable = get(S.Select(idx),'Value');
            if(enable == 1)
                data = str2double(get(S.TypeVal(idx),'String'));
                set(S.popTypeVal(idx),'string',data);
            end
        else    % if popenable is 1
            data = str2double(get(S.popTypeVal(idx),'String'));
        end
    end
end

end

3 个答案:

答案 0 :(得分:0)

如何命名你的数字:

S.Fig1 = figure('name','figure1');

S.Fig2 = figure('name','figure2');

然后您可以使用findobj找到它们:

findobj('type','figure','name','figure2');

如果图形句柄处于打开状态,它将返回图形句柄,如果图形句柄处于关闭状态,则返回空白。因此,此调用将检查该数字是否存在:

~isempty(findobj('type','figure','name','figure2'))

来源:

How to check if a figure is opened and how to close it?

http://www.mathworks.com/help/matlab/ref/findobj.html

http://www.mathworks.com/help/matlab/ref/isempty.html

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 所以我猜你在这一行上收到了错误:set(S.popfh,{'visible'},{'on'});如果图如上所述关闭的话pb1_call。所以不应该这样,你应该:

if ~isempty(findobj('type','figure','name','Pop-Up Window'))
    set(S.popfh,{'visible'},{'on'});
else
    S.popfh=figure('name','Pop-Up Window','Visible','on');
end

这更加强大,并且在尝试更改其属性之前确保图形处于打开状态。

答案 1 :(得分:0)

我不确定我是否理解你的问题。 我想你会在函数之间传递一些变量。如果是这样,您必须使用“guidata”功能。 例如:你已经在一些回调函数(将其命名为MyFunc1)中读取了一些数据(让我们将其命名为DATA),并希望在另一个中使用它。如果是这样,您必须在导出函数的末尾添加这两行代码:

function MyFunc1_Callback(hObject, eventdata, handles)
%
% BODY OF YOUR FUNCTION WHICH CREATES "DATA" VARIABLE
%


% attach DATA to handles structure
handles.DATA = DATA;
% Update handles structure
guidata(hObject,handles);

然后在另一个函数中,您可以使用存储在“handle”结构中的DATA:

function MyFunc2_Callback(hObject, eventdata, handles)
data = handles.DATA;
...

答案 2 :(得分:0)

我找到了问题的答案。 我必须分别定义两个数字的CloseRequestFcn,并且我能够控制我的所有要求

相关问题