How does the canceling callback of waitbar works?

6 Ansichten (letzte 30 Tage)
Xin Wang
Xin Wang am 12 Aug. 2012
I am learning GUI design and puzzled about the mechanism of callback execution of waitbar.
Here is the example in the help of waitbar
function [valueofpi step] = approxpi(steps)
% Converge on pi in steps iterations, displaying waitbar.
% User can click Cancel or close button to exit the loop.
% Ten thousand steps yields error of about 0.001 percent.
h = waitbar(0,'1','Name','Approximating pi...',...
'CreateCancelBtn',...
'setappdata(gcbf,''canceling'',1)');
setappdata(h,'canceling',0)
% Approximate as pi^2/8 = 1 + 1/9 + 1/25 + 1/49 + ...
pisqover8 = 1;
denom = 3;
valueofpi = sqrt(8 * pisqover8);
for step = 1:steps
% Check for Cancel button press
if getappdata(h,'canceling')
break
end
% Report current estimate in the waitbar's message field
waitbar(step/steps,h,sprintf('%12.9f',valueofpi))
% Update the estimate
pisqover8 = pisqover8 + 1 / (denom * denom);
denom = denom + 2;
valueofpi = sqrt(8 * pisqover8);
end
delete(h) % DELETE the waitbar; don't try to CLOSE it.
I use the function approxpi(...) as a callback of another button (name it A) , and it still works! but how?
In "Control Callback Excution and Interuption",it says:
"If a callback is executing and the user triggers an event for which a callback is defined, that callback attempts to interrupt the callback that is already executing. When this occurs, MATLAB software processes the callbacks according to the values of two properties:
The Interruptible property of the object whose callback is already executing. The Interruptible property specifies whether the executing callback can be interrupted. The default value for uicontrol objects is 'on', allowing interruption.
The BusyAction property of the object whose callback has just been triggered and is about to execute. The BusyAction property specifies whether to queue the callback to await execution or cancel the callback. The default property value is 'queue'. "
so ,with the sample code above ,when i click the cancel button of the waitbar, there is already callback running ,so the 'setappdata(gcbf,...)' should have be canceled,and as a result the cancel button should have not worked.
I get confused

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Aug. 2012
It is allowed for one handle graphics callback to interrupt another, provided that the Interruptible and BusyAction properties agree.
Note: waitbar() creates a new figure with its own properties.
  2 Kommentare
Xin Wang
Xin Wang am 12 Aug. 2012
Thank you very much.
I checked the interrupible property of the UIControl whose callback is calling the waitbar in debug mode ,it is 'off'.
but when i put a set(...,'interrupible','off') after the creation and updating line of waitbar,after that,the canceling button of waitbar does not work again~
so ,there may be some work done by matlab behind the scenes
You may have a try :)
Walter Roberson
Walter Roberson am 12 Aug. 2012
Recall that waitbar() creates its own figure, and that is the one whose interruptible property matters. The code gets executed and it sets a value that is then read in the original routine. The original routine is not, in a sense, being interrupted: it is asking for the setappdata() values.
If you do not want a different figure to be able to have its code execute, then you should be setting your figure windowstyle to be 'modal'.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by