How to clear GUI variables?

Hi,
I'm trying to clear GUI variables. From what I understand, each callback function has its own workspace and separate global workspace. I'd like to clear a specific callback variables with a push button. (Callback function is blockremover_callback, I'd like to reset with a push button).
Thanks!
function clearscreen_Callback(hObject, eventdata, handles)
global xy h binary
%evalin('base','clear h')
clear blockremover_callback
xy = [];
h = [];
binary = [];
%evalin('base','clear all');
cla;

 Respuesta aceptada

Image Analyst
Image Analyst el 18 de Dic. de 2013

0 votos

You can clear global variables with clear global.
clear global xy;
clear global h;
clear global binary;
After you clear them, if you need them you need to declare them again:
global xy;
global h;
global binary;
There is no need to clear local variables as they vanish once the function exits.

3 comentarios

Kev
Kev el 18 de Dic. de 2013
Editada: Walter Roberson el 18 de Dic. de 2013
I'm still getting the error after clearing those three variables. My objective is to to allow user to use the tool without closing and reopening for collecting data from several images.
Steps are as follows:
  1. Load image
  2. calibrate it once from the scale
  3. remove messy regions from the image
  4. follow the rest (procedure to get diameters).
After above steps done, user can click a push button to reset all the variables so we don't get mixed data (and to minimize run time errors also). After that, user can load another file, but when we try to use that impoly enclosed region removal function, it is giving error. I initially thought it would have been due to not clearing the variables for second run.
Error using imroi/parseInputsForCreateMask (line 83)
Ambiguous syntax. Associated axes contains more than one image. Specify image handle as input argument
to resolve ambiguity. Type "help imroi/createMask" for more information.
Error in imroi/createMask (line 245)
[obj,h_im] = parseInputsForCreateMask(varargin{:});
Error in FibGUI>blockremover_Callback (line 123)
binary = h2.createMask();
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in FibGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)FibGUI('blockremover_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
------------------------------------------------------------------------------
Code involving this two functions:
function blockremover_Callback(hObject, eventdata, handles)
% hObject handle to blockremover (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global read_image burnedImage xy binary h2
colour_to_gray = rgb2gray(read_image);
filtered_image = medfilt2(colour_to_gray);
axes(handles.axes10)
imshow(filtered_image);
burnedImage = filtered_image;
while true
% some code to do masking
h2 = impoly();
binary = h2.createMask();
xy = h2.getPosition;
burnedImage(binary) = 255;
axes(handles.axes10)
imshow(burnedImage)
% Ask if user wants to do another one.
promptMessage = sprintf('Do you want to Continue masking,\or Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
function binarize_Callback(hObject, eventdata, handles)
% hObject handle to binarize (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Global variables used from other functions or declared as global to be
%used in other functions
global binary read_image ratio_value burnedImage
%colour_to_gray filtered_image
set(handles.edit13, 'String' , ratio_value);
%%%WANT impoly region removal optional.
if (burnedImage ==0)
rgb = read_image;
gray = rgb2gray(rgb);
filtered_gray = medfilt2(gray);
level = graythresh(filtered_gray);
binary = im2bw(filtered_gray, level);
axes(handles.axes10);
imshow(binary);
else
gray2bin = burnedImage;
level = graythresh(gray2bin);
binary = im2bw(gray2bin, level);
axes(handles.axes10);
imshow(binary);
end
Image Analyst
Image Analyst el 18 de Dic. de 2013
Like I said in your other question, if it thinks there is more than one image in an axes, then call cla('reset') before calling imshow() so that the axes is emptied before loading an image into it.
Kev
Kev el 18 de Dic. de 2013
Editada: Kev el 18 de Dic. de 2013
Perfect! Wish thre were emoticons on this forum. :) The problem was with resetting axis. I thought it was something else. I applied cla command in the block. I didn't know there were separate cla('reset') function also. Anyways, Thanks very much!!!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 17 de Dic. de 2013

0 votos

No, there is only one global workspace.
Each function may have its own persistent variables. Those variables may be cleared by using "clear" together with the function name.

1 comentario

Kev
Kev el 18 de Dic. de 2013
Editada: Kev el 18 de Dic. de 2013
Thanks for providing link. That wiki is informative. Bookmarked! :)

Iniciar sesión para comentar.

Productos

Preguntada:

Kev
el 17 de Dic. de 2013

Editada:

Kev
el 18 de Dic. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by