What is wrong with my exit button?

I have this code on an exit button and when I hit the button and select 'yes' to save the data I receive the figure again. Why? What am i doing wrong? And why when I select 'no', I receive the questiondlg again?
answer=questdlg('Save changes to the model?',...
'Exit ADADCS Confirm','Yes','No','Cancel','Cancel');
switch answer
case 'Yes'
[filename,pathname] = uiputfile('default','Save your GUI settings');
if pathname == 0 %if the user pressed cancelled, then we exit this callback
return
end
%construct the path name of the save location
saveDataName = fullfile(pathname,filename);
%saves the gui data
hgsave(saveDataName);
delete(hObject);% Hint: delete(hObject) closes the figure
close all;
case 'No'
delete(hObject);% Hint: delete(hObject) closes the figure
close all;
end

 Réponse acceptée

Paulo Silva
Paulo Silva le 8 Juin 2011

1 vote

I tried your code and it works without any of the problems you describe, they might be caused by other code.
Just two small suggestions, the close all doesn't need the ; at the end, it's common to both Yes and No responses (also the cancel got the return line), you can remove those two "close all" lines put it after the end of that code.

6 commentaires

sadel
sadel le 8 Juin 2011
Did you try to save it with the name: default ?
Paulo Silva
Paulo Silva le 8 Juin 2011
yes and worked, I only added hObject=figure to the start of the code to avoid the error with the hObject. MATLAB 2008b
sadel
sadel le 8 Juin 2011
I use the same code on 'Exit' menubutton and on 'x' button. The first works with problems and the second without. And I removed that you told me. The same...
Paulo Silva
Paulo Silva le 8 Juin 2011
Now I know what's the problem, when you put that code in your button the hObject is the handle of the button not the figure one, in that case delete(hObject) just deletes the button, try delete(handles.figure1) or delete(gcf)
Paulo Silva
Paulo Silva le 8 Juin 2011
Unless you are trying to close several figures you don't need the close all, just use the delete(... to delete the figure after that code.
sadel
sadel le 8 Juin 2011
This is the answer: delete(gcf). thank you!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by