I have a MATLAB GUI which I have created using GUI Layout Toolbox. This GUI has a primary figure. The primary figure has a push button that calls a secondary figure. There is a function called through primary figure, which access some variables from secondary figure. At The beginning of the code or till the point the secondary figure is opened, everything works fine. Once the secondary is opened, if I keep it open, the function call works fine, but if i close the secondary figure, the function call stops working.
Below is a snippet of how I have defined my variables and function calls:
S.Fig1 = figure();
S.var1 = uicontrol('parent',S.Fig1,...
'style','edit');
S.Fig2 = figure();
S.var2 = uicontrol('parent',S.Fig2,...
'style','edit');
S.var1 is associated with function call var1_call() and inside that function I am checking value for S.var2.
If the secondary figure is open, value is provided correctly, else the statement will show an error saying "invalid handle object"
Please let me know if I cannot define the two figures as I have and if I can then how can I check if the fig2 is closed after opening it once.
Thanks
Adding the below example code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Test script to check calling of a new figure from main figure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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