Cancel Button of waitbar doesn't work

7 vues (au cours des 30 derniers jours)
Mathieu
Mathieu le 12 Oct 2015
Modifié(e) : Mathieu le 12 Oct 2015
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.

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Oct 2015
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 commentaires
Walter Roberson
Walter Roberson le 12 Oct 2015
The above code works for me in R2014a exactly as-is.
I do not have R2015a to test with.
Mathieu
Mathieu le 12 Oct 2015
Modifié(e) : Mathieu le 12 Oct 2015
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 12 Oct 2015
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 commentaires
Mathieu
Mathieu le 12 Oct 2015
Modifié(e) : Mathieu le 12 Oct 2015
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 le 12 Oct 2015
Modifié(e) : Mathieu le 12 Oct 2015
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dialog Boxes dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by