MATLAB: Issue with GUI.

guiguideMATLABpushbutton

Hello,
My problem is that in my GUI, I have six pushbuttons with alternating text. I also have a status bar to indicate how much time the user has to select a button. My problem is that the change in text and the status bar is not synced. When I do get it to sync, the status bar is opened in another window, instead of remaining on the GUI with the buttons. By using a pushbutton, I can have the status bar remain on the GUI but it stays filled instead of being synced with the change in text. When I have it synced, I have it part of the timer function callback, but it opens in another window. Can someone please help?

Best Answer

Justin - I think what you can do is create another timer to periodically update the status bar, rather than relying a while loop that is called from within a hidden button (your pushbutton8).
For example, in the GuiTImerUpdateTextExample_OpeningFcn, you can instantiate your two timers, the original one and one for your progress bar as
handles.timerQuery = timer('Name','MyQueryTimer', ...
'Period',3, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerQueryCallback,hObject});
handles.timerProgress = timer('Name','MyProgressTimer', ...
'Period', 0.01, ...
'TasksToExecute',inf, ...
'ExecutionMode', 'fixedSpacing', ...
'TimerFcn', {@timerProgressCallback, hObject});
The "query timer" will expire after three seconds, corresponding to the query that is asked of the user. The "progress timer* will expire every 10 milliseconds and will call the timerProgressCallback. Once we have created the timers, we can initialize your progress axes as
% set the axes
cla(handles.axes1);
set(handles.axes1,'Visible','on','color',[.9 .85 .3]); %this will set the color of the patch
axis(handles.axes1,[0,1,0,1]); %show only from x=0to 1 and y=0to1
handles.hPatch = patch([0,0,0,0],[0,0,1,1],'g');
axis(handles.axes1, 'off'); %do not show axis bars
Note how we create the patch and save its handle to the handles structure. We do this so that we can update this patch object rather than creating a new one every time we want to update the progress "bar".
We then save the updated handles structure and start the the query timer as
guidata(hObject, handles);
start(handles.timerQuery);
We don't start the other timer as we rely on the timerQueryCallback to do this for us as
function timerQueryCallback(hTimer, eventdata, hFigure)
handles = guidata(hFigure);
% stop the progress timer
stop(handles.timerProgress);
% only set the progress timer to complete if there is text in pushbutton1
% we pause for half a second to allow the progress bar to update
if ~isempty(get(handles.pushbutton1,'String'))
set(handles.hPatch,'XData',[0 1 1 0]);
pause(0.5);
end
% etc.
% reset the patch to show zero progress
set(handles.hPatch,'XData',[0 0 0 0]);
% use tic so that we can correctly update the progress
tic;
% start the timer
start(handles.timerProgress);
That just leaves the timerProgressCallback which can be defined as
function timerProgressCallback(hObject, eventdata, hFigure)
handles = guidata(hFigure);
if ~isempty(handles)
periodQuery = get(handles.timerQuery,'Period');
percentComplete = double(toc)/periodQuery;
set(handles.hPatch,'XData',[0 percentComplete percentComplete 0]);
end
We use toc and the period of the query timer to determine the progress and so how much of the patch to draw.
Try the above and see what happens! (See the attached for a working example.)