Displaying graphics in App Designer requires a different workflow than you typically use at the MATLAB® command line. Once you understand this workflow and a few special cases, you will know how to call the functions you need for displaying almost any type of plot.
Many of the graphics functions in MATLAB (and MATLAB toolboxes) have an argument for specifying the target axes or parent
object. This argument is optional in most contexts, but when you call these functions in
App Designer, you must specify that argument. The reason is that, in most contexts,
MATLAB defaults to using the gcf
or gca
functions to get the target object
for an operation. However, the HandleVisibility
property of App
Designer figures is set to 'off'
by default. This means that
gcf
does not return an App Designer figure, and
gca
does not return any axes within an App Designer figure. As
a result, omitting the argument for a target axes or parent object can produce
unexpected results.
This code shows how to specify the target axes when plotting two lines. The first
argument passed to plot
and hold
is app.UIAxes
, which is the default name for the
App Designer
axes.
plot(app.UIAxes,[1 2 3 4],'-r'); hold(app.UIAxes); plot(app.UIAxes,[10 9 4 7],'--b');
Some functions (such as imshow
and triplot
) use a name-value pair argument to specify the target object.
For example, this code shows how to call the imshow
function in App
Designer.
imshow('peppers.png','Parent',app.UIAxes);
You can create most 2-D and 3-D plots using the App Designer axes (a uiaxes
object). Starting in R2018b, you can create additional plots,
such as those listed in the following table. Most of these plots require a different
type of parent object and additional lines of code in your app. All of them use
normalized position units by default.
Functions | Coding Details |
---|---|
polarplot polarhistogram polarscatter compass | Create the polar axes by calling the
theta = 0:0.01:2*pi; rho = sin(2*theta).*cos(2*theta); pax = polaraxes(app.Panel); polarplot(pax,theta,rho) Or, create a compass plot in a similar way: rng(0,'twister')
M = randn(20,20);
Z = eig(M);
app.Axes = axes(app.Panel);
compass(app.Axes,Z) |
subplot | Follow these steps:
For example: app.UIFigure.AutoResizeChildren = 'off'; ax1 = subplot(1,2,1,'Parent',app.UIFigure); ax2 = subplot(1,2,2,'Parent',app.UIFigure); plot(ax1,[1 2 3 4]) plot(ax2,[10 9 4 7]) |
tiledlayout | Create a tiled chart layout in a panel and create axes in
it using the t = tiledlayout(app.Panel,2,1); [X,Y,Z] = peaks(20) % Tile 1 ax1 = nexttile(t); surf(ax1,X,Y,Z) % Tile 2 ax2 = nexttile(t); contour(ax2,X,Y,Z) |
pareto plotmatrix | Follow these steps:
For example: app.UIFigure.AutoResizeChildren = 'off';
ax = axes(app.UIFigure);
pareto(ax,[10 20 40 40]) |
geobubble heatmap parallelplot scatterhistogram stackedplot wordcloud | Specify the parent container when calling these functions
(for example, For example: h = heatmap(app.UIFigure,rand(10)); |
geoplot geoscatter geodensityplot | Create the geographic axes by calling the
latSeattle = 47 + 37/60; lonSeattle = -(122 + 20/60); gx = geoaxes(app.UIFigure); geoplot(gx,latSeattle,lonSeattle) |
Starting in R2020a, annotations created with the annotation
function are supported in App Designer figures. To create an
annotation, call the annotation
function, specifying the UI figure as
the first input argument. For example:
x = [0.3 0.5];
y = [0.6 0.5];
annotation(app.UIFigure,'arrow',x,y)
As of R2020a, some graphics functionality is not supported in App Designer. This table lists the functionality that is relevant to most app building workflows.
Category | Not Supported |
---|---|
Animation | |
Retrieving and Saving Data | Instead of the Figures created programmatically with
|
Utilities | Instead of the |
Functions not Recommended | |
Axes in Grid Layout Managers or Scrollable Containers | Workarounds:
|
Properties |
|
UI Figure Properties | UIAxes Properties