Filter löschen
Filter löschen

Clearing subplots in MATLAB gui

10 Ansichten (letzte 30 Tage)
Gee Cheng Mun
Gee Cheng Mun am 30 Jan. 2016
Bearbeitet: Gee Cheng Mun am 30 Jan. 2016
I have trouble clearing all the subplots in my GUI axes. At first, I thought of creating more axes, but I wanted to save all the subplots as a single image as well. Is there a way to clear all the subplots within an axes?
Below is my code:
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
handles.hSubplot1=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);
yellowMask = uint8(redMask & greenMask);
%Count pixels
redCount=sum(redMask(:)>0);
greenCount=sum(greenMask(:)>0);
yellowCount=sum(yellowMask(:)>0);
handles.hSubplot2=subplot(2, 2, 2);
imshow(redMask, []);
title(['Red Pixels = ' num2str(redCount)]);
handles.hSubplot3=subplot(2, 2, 3);
imshow(greenMask, []);
title(['Green Pixels = ' num2str(greenCount)]);
handles.hSubplot4=subplot(2, 2, 4);
imshow(yellowMask, []);
title(['Yellow Pixels = ' num2str(yellowCount)]);
% --- Executes on button press in reset2.
function reset2_Callback(hObject, eventdata, handles)
cla(handles.axes3);
if isfield(handles, 'hSubplot1')
delete(handles.hSubplot1);
end

Antworten (1)

Walter Roberson
Walter Roberson am 30 Jan. 2016
subplots are not within an axes: subplots are axes, and axes cannot be nested.
subplots are contained within a figure.
cla( allchild(get(handles.axes3, 'parent'),'type','axes') )
Caution: if you are using R2014a or earlier, legend() and colorbar() are implemented as axes so they would be cleared as well with the above code.
  5 Kommentare
Walter Roberson
Walter Roberson am 30 Jan. 2016
parent_container = ancestor(hObject, {'uipanel', 'figure', 'uitab'});
axes_contained = findall(parent_container, 'type', 'axes');
cla( axes_contained );
You see, you have not given us enough information to know which axes to be cleared. subplot() works within a container object, but container objects for axes can be uipanel or can be figures or can be uitab, and we do not know which level you are working with. We also do not know which of those containers you are working on, since any GUI can have multiple uipanel or uitab on any given figure and can have multiple figures.
With all this lack of information, we make the educated guess that you probably put your reset button in the same container object that the plots are in. So we look to find out what container object holds the reset button. Then we look downwards from the container object to find all of the axes that the container has inside it. And then we clear those.
We do run the risk that you used GUIDE to create a uitab or uipanel in R2014a or earlier and failed to notice that you were not putting the plots inside the container (because the uicontrols were always shown on top of graphics there even when something was over them, so it was easy to miss that the parent was wrong.) But we have to start somewhere. We could have just assumed that all axes everywhere in all of your figures were to be cleared, but that seemed a little unfriendly.
It would have been much much easier to do all of this if you had just recorded all of the subplot axes handles and asked to clear those specifically, instead of us having to probe around the graphics and make guesses about which axes were to be cleared...
Gee Cheng Mun
Gee Cheng Mun am 30 Jan. 2016
Bearbeitet: Gee Cheng Mun am 30 Jan. 2016
I am so sorry for the trouble. I have no idea that I wasn't giving enough information. The container object for my axes is actually figure. I use subplot() to display the original colored image and 3 other images in the same figure. So, I would like to clear all of them at the same, so I can continue to other images. How do I record all the subplot axes handles and clear them specifically? Can you give me an example on how to do so?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by