【发布时间】:2023-03-13 06:34:02
【问题描述】:
我有一个使用 GUI 布局工具箱创建的 MATLAB GUI。这个 GUI 有一个主要的图形。主要人物有一个按钮,可以调用次要人物。有一个通过主要图形调用的函数,它从次要图形访问一些变量。在代码的开头或直到打开第二个数字时,一切正常。一旦打开了辅助图形,如果我保持打开状态,函数调用就可以正常调用,但是如果我关闭辅助图形,函数调用将停止工作。
下面是我如何定义变量和函数调用的 sn-p:
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
【问题讨论】:
标签: matlab matlab-guide