Clear Filters
Clear Filters

Simply entering the debugger during execution of MATLAB GUI fixes error that persists during normal execution

6 views (last 30 days)
I'm using a programmatic GUI in MATLAB which uses multiple figure windows. When I press the button in Window 1, Window 2 is created with some data plotted. For every subsequent button press, the data in Window 2 are refreshed. I want the focus to immediately switch back to Window 1 after every button press because there are hotkeys (not present in this simple example) that I use in that window to update the plots.
There are 2 problems:
1) Focus switches back to Window 1 only if Window 2 already exists. When Window 2 is first created, it remains in focus.
2) Simply setting a breakpoint within the button callback and then resuming execution solves the problem, but it persists during normal execution.
What is happening here?
Please see the minimal working example below:
function heisenbug
%%Main problem:
% After clicking the push button, I want the focus to
% always switch back to Window 1 (the one containing the button).
% However, this does not work when Window 2 is first created.
%%Create and then hide the GUI as it is being constructed
f = figure('Visible','off','name','Window 1','units','normalized','Position',[0.1 0.1 0.5 0.5]);
%%Initialize handles structure
handles = guihandles(f);
handles.window2 = [];
guidata(f,handles)
%%Make a button
hbutton = uicontrol('Style','pushbutton','String','Push me','units','normalized',...
'Position',[0.1 0.1 0.8 0.8],...
'Callback',@button_Callback);
%%Make the GUI visible
f.Visible = 'on';
%%Button callback
function button_Callback(source,eventData)
handles = guidata(gcbo);
% If Window 2 already exists, plot a circle, then switch focus back to Window 1.
if ~isempty(handles.window2) && ishandle(handles.window2)
figure(handles.window2);
plot(1,1,'bo')
figure(f);
% Otherwise, create Window 2 and do the same thing.
else
handles.window2 = figure('Name','Window 2','units','normalized','position',[0.4 0.1 0.5 0.5]);
plot(1,1,'bo')
figure(f)
end
guidata(source,handles)
end
end

Accepted Answer

Adam
Adam on 15 Aug 2016
Have you tried inserting a short pause instruction e.g.
pause( 0.5 )
between plotting and setting the figure f to be active?
I think this is probably an issue that the code executes the figure(f) instruction before the launching of the second figure and plotting on it has completed and so when it does complete it steals back the focus.
Obviously with a breakpoint you have a natural pause so this does not happen.
  2 Comments
Benjamin
Benjamin on 15 Aug 2016
thank you!! this has been driving me crazy. would you please explain how figure(f) is called before the lines above it are finished? i thought the code was executed in a strictly serial manner, so this doesn't make sense to me.
Adam
Adam on 15 Aug 2016
I'm not sure exactly why, the code is executed in a serial manner, but the actual rendering of the figure takes time even after the code to launch it has completed and this is where the difference comes in though it is not really something I know too much about, just something I have noticed myself too.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!