Create a "Pause" button in the waitbar function - GUI

11 views (last 30 days)
Hello everyone,
I am looking for a way to add a Pause button to the waitbar function in a Matlab GUI. Unfortunately the waitbar function does only support a "CreateCancelBtn" Option. When I use uicontrol to create another button it opens a new window which only includes the one button and opens the waitbar seperately. How can I get another button on the left side right next to the cancel button then?
This is my code so far:
function stat = waitbar
h = waitbar(0,'Progress','Name','Waitbar',...
'CreateCancelBtn',...
'setappdata(gcbf,''canceling'',1)');
p = uicontrol('Style', 'pushbutton', 'String', 'Pause',...
'Position', [20 20 50 20],...
'Callback', 'pause=1');
step = 100;
setappdata(h,'canceling',0)
for k = 1:step
if getappdata(h,'canceling')
break
end
disp(k);
pause(.1);
waitbar (k/step)
end
stat = 'done';
delete(h)
end
Thanks to everyone in advance!

Answers (1)

Geoff Hayes
Geoff Hayes on 8 May 2016
alty - naming your function waitbar which is the same name as the built-in MATALB function is not a good idea, so you will want to rename it to something else (perhaps, waitbarWithPause). In fact, if I try to run your code, I observe the error
Error using waitbar
Too many input arguments.
Error in waitbar (line 2)
h = waitbar(0,'Progress','Name','Waitbar',...
Which makes sense since because your waitbar function does not accept any inputs and yet line 2 tries to pass in several.
If you want to include a pause button next to the cancel button, then I one way is to create a custom waitbar tool. Through either GUIDE (or programmatically) you would need to create a simple GUI with a progress bar (you could use an axes) with two push buttons. See http://www.mathworks.com/matlabcentral/answers/267514-how-to-merge-the-waitbar-into-uicontrol-gui which may provide you with an idea of how to create your own waitbar.
An alternative is to add the button yourself by creating the button (much like you are doing already) but specifying the parent as the waitbar. We then shift this button to the left of the Cancel button by using its position.
function waitbarWithPauseBtn
h = waitbar(0,'Progress','Name','Waitbar',...
'CreateCancelBtn',@cancelButtonCallback);
hChildren = get(h,'Children');
for k=1:length(hChildren)
hChild = hChildren(k);
if isfield(get(hChild),'Style') && strcmpi(get(hChild,'Style'),'pushbutton')
hChildPos = get(hChild,'Position');
pauseBtnPos = hChildPos;
pauseBtnPos(1) = pauseBtnPos(1) - pauseBtnPos(3) - 1;
hPauseBtn = uicontrol(h, 'Style', 'pushbutton', 'String', 'Pause',...
'Position', pauseBtnPos,...
'Callback', @pauseButtonCallback);
end
end
step = 100;
k = 1;
pauseWaitbar = 0;
cancelWaitbar = 0;
while k < step && ~cancelWaitbar
pause(.1);
if ~pauseWaitbar
waitbar(k/step);
k = k + 1;
end
end
delete(h)
function pauseButtonCallback(hSource, eventdata)
pauseWaitbar = ~pauseWaitbar;
end
function cancelButtonCallback(hSource, eventdata)
cancelWaitbar = 1;
end
end
We iterate over each child object of the waitbar until we find the pushbutton. We then get its position and set the pause button to the left. The important step is when we create the uicontrol and we pass in h which is the parent for the new button.
As our callbacks are nested, we can avoid the use of getappdata or setappdata and just reference the local variables that have been defined in the main function. Try the above and see what happens!
  2 Comments
alty
alty on 8 May 2016
Thanks so much! It worked and i changed a few details. To be honest I am not completely sure what happened in the first paragraph.
could you please explain a little further how this works:
% I guess in here it just creates the Child Node
hChildren = get(h,'Children');
% I am not sure what happens in the next 2 lines
for k=1:length(hChildren)
hChild = hChildren(k);
% I guess in here you are searching for the field "style" in the fild of "pushbutton"s ?
if isfield(get(hChild),'Style') && strcmpi(get(hChild,'Style'),'pushbutton')
hChildPos = get(hChild,'Position');
pauseBtnPos = hChildPos;
% I specified the width and height etc... in pixels here. Can you go in more detail about this?
pauseBtnPos(1) = pauseBtnPos(1) - pauseBtnPos(3) - 1;
% Here it creates the button
hPauseBtn = uicontrol(h, 'Style', 'pushbutton', 'String', 'Pause',...
'Position', pauseBtnPos,...
'Callback', @pauseButtonCallback);
end
end
Since I am new to this I hope you can excuse all my many questions. ;-)
Geoff Hayes
Geoff Hayes on 8 May 2016
alty - here's some more context surrounding each line
hChildren = get(h,'Children');
Since h is the handle to the waitbar, the get will get all Children graphics objects (like the cancel pushbutton, the axes for the progress bar, etc). The get method when used with a property (in this case Children) returns the element or array of elements for that property.
for k=1:length(hChildren)
hChild = hChildren(k);
Since hChildren is an array, the length function returns the number of elements in this array which we then iterate over using the for loop. hChild is the kth element in the hChildren array.
The if conditions ensure that we find the Cancel button (since that is the control we are interested in).
hChildPos = get(hChild,'Position');
is a four element array for the position, height, and width of the Cancel button.
pauseBtnPos(1) = pauseBtnPos(1) - pauseBtnPos(3) - 1;
The first element in the pauseBtnPos array is the x coordinate and the third element is the width (for the Cancel button). We then subtract the width from the x coordinate so that the Pause button is shifted to the left (all other elements in this array remain the same).
Try stepping through the code using the MATLAB debugger. That will reveal more of what is going on.

Sign in to comment.

Categories

Find more on App Building 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!