Cancel Button of waitbar doesn't work

4 次查看(过去 30 天)
Hello.
After some researches on internet, and no solution found yet, I come here hoping you will be able to help me.
In my GUI, I have some calculations launched after the opening of a file. In order to see the progression, I created a waitbar in which I add a cancel button (to cancel the calculation if the operator want it). In this way, I created the waitbar like this:
barre = waitbar(0, 'Récupération des données', ...
'Name', 'Treatment', ...
'CreateCancelBtn', 'setappdata(gcbf, ''canceling'', 1)');
and when I want to see if the operator clicked on the cancel button, I do:
flag_cancel = getappdata(barre, 'canceling');
if flag_cancel
return
else
do some code
end
But, each time I tried to retrieve the canceling value, I have an empty value like this my click on the cancel button was never taken into account.
I tried several methods, as replace gcbf by the handle of a figure, but always not working. Besides, I tried to add a function to replace the setappdata:
barre = waitbar(0, 'Récupération des données', ...
'Name', 'Treatment', ...
'CreateCancelBtn', {@toto});
creating a function toto in another .m file
function toto(obj, event)
disp('hi!')
end
But here my program never go into this function when I click on the cancel button. The most surprising is that I never go into this toto function.
Any ideas?
I thank you in advance.

采纳的回答

Walter Roberson
Walter Roberson 2015-10-12
In my test in R2014a it calls the anonymous function I created.
barre = waitbar(0, 'Récupération des données', ...
'Name', 'Treatment', ...
'CreateCancelBtn', @(obj, event) disp('hi') );
  3 个评论
Walter Roberson
Walter Roberson 2015-10-12
The above code works for me in R2014a exactly as-is.
I do not have R2015a to test with.
Mathieu
Mathieu 2015-10-12
编辑:Mathieu 2015-10-12
I agree with you, when I try with a simple example I can put any function for the cancel button, it is working. But with my GUI I don't understand why it isn't because I don't do something special with this bar, only a refreshing of the text...
Thank you anyway for your time.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2015-10-12
Using strings as callbacks is kept for backward compatibility since Matlab R6.5. Better use the "modern" method of function handles in every case.
setappdata(gcbf, ''canceling'', 1)
This sets the ApplicationData of the waitbar's figure. What is the value of the variable barre? Is this the handle of the GUI or the waitbar?
It should work, if the cancel button changes the Applicationdata of the GUI, but givt the GUI a chance to update by adding a drawnow in the function, whose function handle is stored in the canceling function of the waitbar.
  2 个评论
Mathieu
Mathieu 2015-10-12
编辑:Mathieu 2015-10-12
barre is the handle of the figure which contians the waitbar (first line of code in my post).
For information:
- function A read the data and create the waitbar, which the handle is a global variable
- function B filter the data and modify the text in the waitbar
- function C treat the data, modify the text of the waitbar, and look if the the cancel button is pressed or not
To change the text of the waitbar, I do:
waitbar(pourcentage, barre, 'New text');
So I don't create a new waitbar at each update. I don't see where I have to add the drawnow function.
And also with a simple example it is working on my computer, it is just with my GUI I have some troubles.
Mathieu
Mathieu 2015-10-12
编辑:Mathieu 2015-10-12
Finally I saw that whatever the function I put for the cancel button, the program doesn't execute it. Also with an anonymous function.
I am always looking for the origin of the problem.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dialog Boxes 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by