Error in reset push button in GUI

1 view (last 30 days)
Gee Cheng Mun
Gee Cheng Mun on 15 Jan 2016
Commented: Geoff Hayes on 16 Jan 2016
I wanted to install a reset button in gui, by using the code below. The function (select_pet_image) was able to reset, however the other function (color_segmentation) was not able to do so. Thus, I am just able to perform this GUI for one image, that's all. My code is as shown below.
function select_pet_image_Callback(hObject, eventdata, handles)
axes(handles.axes3);
path='G:\FYP2\Time Frame\';
filter='*.png';
[selectedFile, selectedPath]=uigetfile(fullfile(path, filter));
A=imread([selectedPath, selectedFile]);
[rows, columns, numberOfColorBands] = size(A);
imshow(A);
title('PET scan Image');
setappdata(handles.select_pet_image, 'Select_a_PET_image', A);
% --- Executes on button press in color_segmentation.
function color_segmentation_Callback(hObject, eventdata, handles)
axes(handles.axes3);
data=getappdata(handles.select_pet_image, 'Select_a_PET_image');
[rows, columns, numberOfColorBands] = size(data);
if numberOfColorBands > 1
subplot(2, 2, 1);
imshow(data);
title('Original Color Image');
else
caption=sprintf('Original Indexed Image\n(converted to true color with its stored colormap)');
title(caption);
end
%EXTRACT OUT THE COLOR BANDS FROM THE ORIGINAL IMAGE
redBand=data(:,:,1);
greenBand=data(:,:,2);
%THRESHOLD LEVELS
redThresholdLow=215;
redThresholdHigh=255;
greenThresholdLow=220;
greenThresholdHigh=255;
redMask=(redBand>=redThresholdLow) & (redBand<=redThresholdHigh);
greenMask=(greenBand>=greenThresholdLow) & (greenBand<=greenThresholdHigh);
subplot(2, 2, 2);
imshow(redMask, []);
title('Red Pixels');
subplot(2, 2, 3)
imshow(greenMask, []);
title('Green Pixels');
yellowMask = uint8(redMask & greenMask);
subplot(2, 2, 4);
imshow(yellowMask, []);
title('Yellow Pixels');
% --- Executes on button press in reset2.
function reset2_Callback(hObject, eventdata, handles)
cla(handles.axes3);

Answers (1)

Geoff Hayes
Geoff Hayes on 15 Jan 2016
Looking at your code, when the color_segmentation_Callback function is called, the following line is executed
axes(handles.axes3);
which sets the current axes to axes3. Later, within this same callback, the code calls subplot four times to create four new axes. Is this what you have intended - to replace the axes3 with four new ones? Because that would explain why your reset does not clear the axes created in color_segmentation_Callback since the reset code only calls cla on axes3 and not on any of the four new axes.
You would need to save the handle to each of these four subplots, something like
handles.hSubplot1 = subplot(2,2,1);
% draw stuff
handles.hSubplot2 = subplot(2,2,2);
% draw stuff
handles.hSubplot3 = subplot(2,2,3);
% draw stuff
handles.hSubplot4 = subplot(2,2,4);
% draw stuff
% save the updated handles structure
guidata(hObject,handles);
Then, in the reset function, call cla on each of these
function reset2_Callback(hObject, eventdata, handles)
cla(handles.axes3);
if isfield(handles,'hSubplot1')
cla(handles.hSubplot1);
end
% etc.
As an aside, you may want to update your GUI so that you have four axes, three of which are hidden and only made visible when you call color_segmentation_Callback. Else you will have difficulties when showing one axes or four.
  4 Comments
Gee Cheng Mun
Gee Cheng Mun on 16 Jan 2016
I understand how to use the function better now, thanks to your attachment! However, if I create new axes and kept them hidden till I run my color_seg code. Won't there be an empty space in my gui?
Geoff Hayes
Geoff Hayes on 16 Jan 2016
Yes, but the alternative is dynamically creating new ones within your GUI (which is what you were doing). Aren't you concerned that the new axes may be positioned incorrectly and so conflict with other controls (i.e. your pushbuttons)?
You could always create the four axes (with three hidden) but have your first axes sized such that it fills the empty space. When the user presses the colour segmentation button, axes1 is resized to fit the space occupied by four axes. Then, when the user presses reset, the axes is full-size again.
Note the attached (rough) example. Look in the code to where we set a "small" and "large" position array for axes1.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!