Why is GUI window closed after push the pushbutton?

Dear all,
I would like to create GUI for image segmentation. But I have this problem: When I click on the pushbutton1 (Načtení skenu) to load the original image then entire GUI closes and no variables are saved in workspace.
I attach my GUI. Thank you for your answers.

Respuestas (1)

Adam
Adam el 9 de Feb. de 2017
Editada: Adam el 9 de Feb. de 2017
clc;
close all;
clear;
Why would you ever put these in a pushbutton callback?
close all is what closes your GUI, clear has the potential to do unspeakable things that will render your GUI useless if it did stay open (and serves no purpose since there are only 3 variables in your workspace at that point) and clc has no place in a GUI callback either.

7 comentarios

Jan
Jan el 9 de Feb. de 2017
+1. The only situation in which this brute clearing header is useful, if writing a script as a short hack and having no time to make in clean. But this should never occur in productive code.
Okay, but still no variable is not saved to the workspace. So, when I clicked pushbutton2 (Histogram skenu) then program writes error:
Undefined function or variable 'grayImage'.
Error in DP_segment_final>pushbutton2_Callback (line 132)
imhist(grayImage, 256);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in DP_segment_final (line 44)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)DP_segment_final('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Adam
Adam el 9 de Feb. de 2017
Your GUI has its own workspace. Use that rather than the base workspace if you want to do work related to your GUI otherwise why use a GUI at all?
There are numerous examples here:
Veronika
Veronika el 9 de Feb. de 2017
Editada: Veronika el 9 de Feb. de 2017
But I usually always had loaded normally figure to workspace and worked with it in GUI. So I don´t know, where is problem.
For example this:
load ('thorax-mdl.fig')
You may have loaded some image into grayImage in some other function, but that is a local copy. This function won't know the other function's grayImage unless you do something to make it known since all variables in a function are local, unless you make them otherwise. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
I used the function
assignin('base', 'grayImage',grayImage );
for saving variables from GUI´s workspace to base workspace, but still there is an error for calling this variable (grayImage) after click on pushbutton2 (Histogram skenu). I attach my code and original image.
Jan
Jan el 14 de Feb. de 2017
Editada: Adam el 14 de Feb. de 2017
@Veronika: Please read the link posted by Image Analyst again. It does not help to store the variable grayImage in the base workspace, because accessing it from the command window is not wanted. If you need this variable inside a GUI callback, you have to store it inside the GUI:
function pushbutton1_Callback(hObject, eventdata, handles)
...
handles.grayImage = grayImage;
guidata(hObject, handles);
end
function pushbutton2_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % Get newest version
% The one from the inputs might be outdated - perhaps.
imhist(handles.grayImage, 256);
This is confusing and therefore it is one of the most frequently asked questions. Search in this forum for "share data between callbacks" to find hundreds of corresponding threads.
Please open a new thread for a new question in the future. Thanks.

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Feb. de 2017

Editada:

el 14 de Feb. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by