MATLAB Answers

Interplay between .Visible='off' and axis(app.UIAxes, 'off') in App Designer

101 views (last 30 days)
David Aronstein
David Aronstein on 22 Oct 2019
Answered: Benjamin Kraus on 11 Nov 2020
This is somewhere between a bug report and a request for help.
In App Designer on R2019a, I am seeing a confusing thing happen where executing an "axis off" command on UIAxes also sets the .Visible field to 'off' without actually making the plot invisible. I noticed this when I had a plot with "axis off" and which I want to appear and disappear at different times in the app execution, and that was getting all mixed up. See simple demo code below.
Is there a way to do the same thing as "axis off" -- meaning, removing x labels, ticks, and tick labels -- without setting off .Visible = 'off' in the presence of this issue/bug?
classdef test_visible < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
x = linspace(0,2*pi,100);
y = sin(x);
plot(app.UIAxes, x, y, '-k');
fprintf('Visibility = %s\n', app.UIAxes.Visible);
axis(app.UIAxes, 'off');
fprintf('Visibility = %s\n', app.UIAxes.Visible);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
app.UIAxes.Position = [33 53 530 392];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = test_visible
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Answers (2)

Hope Q
Hope Q on 28 Oct 2019
I believe that you are not seeing the changes because they are all in the startup function. I adjusted the startup function with drawnow (to force an update draw of the figure within startup) and a pause to give it time to show.
function startupFcn(app)
x = linspace(0,2*pi,100);
y = sin(x);
plot(app.UIAxes, x, y, '-k');
fprintf('Visibility = %s\n', app.UIAxes.Visible);
app.UIAxes.Visible = 'off';
fprintf('Visibility = %s\n', app.UIAxes.Visible);
drawnow('nocallbacks')
pause(1)
app.UIAxes.Visible = 'on';
fprintf('Visibility = %s\n', app.UIAxes.Visible);
end

Benjamin Kraus
Benjamin Kraus on 11 Nov 2020
This is a long-standing behavior of axes that was inherited by UIAxes.
  • On most graphics objects, Visible does what you think it should do: If you set Visible to 'off', the object and all its children become invisible.
  • On axes, when you set Visible to 'off', the axes disappears, but its children remain visible. This is designed so you can hide the axes background, tick labels, and other aspects of the axes, but keep showing the graphics that are inside the axes. This is often used for "world building" type applications.
The easiest way to make the axes and all its children invisible is to put the axes into a panel, and then hide the panel.
p = uipanel;
ax = axes(p);
p = plot(ax, 1:10);
ax.Visible = 'off'; % Line still visible
p.Visible = 'off'; % Line disappears
p.Visible = 'on'; % Line reappears
Alternatively, if you are running R2019b or later, you can also use the tiledlayout command like you would a panel.
t = tiledlayout(1,1);
ax = axes(t);
p = plot(ax, 1:10);
ax.Visible = 'off'; % Line still visible
t.Visible = 'off'; % Line disappears
t.Visible = 'on'; % Line reappears

Community Treasure Hunt

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

Start Hunting!