How to control the appearance of child objects in an axis while working with GUI?
8 views (last 30 days)
Show older comments
Gopinath Karuppannan
on 31 Aug 2021
Commented: Gopinath Karuppannan
on 1 Sep 2021
Hi
I am creating a GUI which has different number of axis handles. I am controlling its appearance through pushbutton callback. When i try to change the axis visibility to 'off', its parent axis becomes invisible whereas the the child axis objects are still in visible state. Can someone suggest how to control the visibility of child objects.
FYI:
In below reference, the left side (handles.axes1) of the axis will be displayed when i call the pushbutton 1. And when i try to call the pushbutton 2 the handles.axes1 will be 'off' (visibility) and the remaining handles.axes will be 'on'(visibility). But in contrary, the handles.axes1 also displayed even i use the following command:
set(handles.axes1,'Visible','off'); % pusbutton 2 callback
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/725379/image.png)
0 Comments
Accepted Answer
Image Analyst
on 31 Aug 2021
Try this demo and I think you will understand how to do it:
% Demo to show how to show or hide an entire axes control.
% By Image Analyst
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
fontSize = 13;
format short g
fprintf('Beginning to run %s.m ...\n', mfilename);
% Create sample data
x = 1 : 10;
y1 = rand(1, 10);
plot(x, y1, 'r.-', 'LineWidth', 2, 'markerSize', 15);
hold on;
y2 = rand(1, 10);
plot(x, y2, 'b.-', 'LineWidth', 2, 'markerSize', 15);
grid on;
title('Title');
xlabel('x');
ylabel('y');
legend('y1', 'y2');
% Hide everything on the axes:
promptMessage = sprintf('Click OK to hide the axes.');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if contains(buttonText, 'Cancel', 'IgnoreCase', true)
return; % or break or continue.
end
ShowOrHideAxes(gca, false)
% Show/restore everything on the axes:
promptMessage = sprintf('Click OK to show the axes.');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if contains(buttonText, 'Cancel', 'IgnoreCase', true)
return; % or break or continue.
end
ShowOrHideAxes(gca, 'on')
%=====================================================================
% Sets visibility of all objects in the specified axes.
% Sample calls:
% ShowOrHideAxes(gca, 'off');
% ShowOrHideAxes(gca, false);
% ShowOrHideAxes(gca, 'on');
% ShowOrHideAxes(gca, true);
function ShowOrHideAxes(h, visible)
if ischar(visible)
if strcmpi(visible, 'off')
visible = false;
else
visible = true;
end
end
if visible
vis1 = 'on';
vis2 = true;
vis3 = 'show';
else
vis1 = 'off';
vis2 = false;
vis3 = 'hide';
end
axis(vis1); % Turn off axes and grid lines.
h.Toolbar.Visible = vis1; % Turn off/on popup toolbar in upper right of axes.
legend(vis3); % Turn off/on legend.
axesHandlesToChildObjects = findall(h);
% If you want to show or hide only plot lines (data) you can do
% axesHandlesToChildObjects = findobj(h, 'Type', 'line');
if ~isempty(axesHandlesToChildObjects)
numObjects = length(axesHandlesToChildObjects);
for k = 1 : numObjects
axesHandlesToChildObjects(k).Visible = vis2;
end
end
return; % from ShowOrHideAxes
end
More Answers (0)
See Also
Categories
Find more on Specifying Target for Graphics Output 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!