How to align axes in AppDesigner
이전 댓글 표시
Hi Folks,
In AppDesigner, is there a simple way to code up a PlotAll(app) function that can easily align vertically the x-axes for vertically stacked axes that have the same "Position" coordinates? For example, below is an example where I have three aligned UIAxes that have the same pixel horizontal positions and thicknesses, but when I plot, the axes labels and the magnitude of the y-axis data values end up causing the plot internal axes to reposition themselves and be mis-aligned.
Thank you,
Kris

댓글 수: 4
Rik
2024년 12월 7일
Can you post example code so we can reproduce it here? That will make playing around with the properties a lot easier.
Kristoffer Walker
2024년 12월 7일
편집: Kristoffer Walker
2024년 12월 7일
Kristoffer Walker
2024년 12월 7일
Kristoffer Walker
2024년 12월 8일
편집: Kristoffer Walker
2024년 12월 8일
채택된 답변
추가 답변 (1개)
Using a <https://www.mathworks.com/help/matlab/ref/tiledlayout.html tiledlayout> would do this for you without any additional position calculus.
You would only need to specify a uipanel as the parent of your tiled layout. See the example below.
Something to consider when using UI axes is that it is essientially a UIPanel and axes together (I think I read that on a mathworks answer one time). Using something like tiledlayout and regular axes is usually better performance (initial creation, and reactivity) and it can do everything a UIAxes can do.
One difference is you do have to programatically asign callbacks, unlike UIAxes that you can do in the app designer.
clearvars
% data
x1=0:.1:1;
y1=x1;
y2=100*y1;
% tiled layout
% p = some parent like a uipanel
% t = tiledlayout(p,'vertical')
t=tiledlayout('vertical');
% first axis
ax=nexttile(t);
plot(ax, x1, y1)
ylabel(ax,'Label1 left')
hold(ax,'on')
yyaxis right
plot(ax,x1,y2)
ylabel(ax,'Label1 right')
ylim(ax,[0 10]) % just to show both lines
% next axis
ax2=nexttile(t);
plot(ax2, x1, y2)
ylabel(ax2,'Label2 left')
hold(ax2,'on')
yyaxis right
plot(ax2,x1,y1)
ylabel(ax2,'Label2 right')
ylim(ax2,[0 10]) % just to show both lines
% insert push button callback to your app to add / delete axes
댓글 수: 9
Kristoffer Walker
2024년 12월 8일
Strider
2024년 12월 8일
I have a strong feeling the answer is yes it will work.
I updated the answer with an example but I guess the code does not execute on a phone. I will amend it later if you do not get to it first.
Strider
2024년 12월 8일
I updated, let me know what you think.
Kristoffer Walker
2024년 12월 9일
편집: Kristoffer Walker
2024년 12월 9일
"Parent container, specified as a Figure, Panel, Tab, or TiledChartLayout object."
I tried to run your example and it was missing x1 or any of the plotting data.
I did not see an error pasted in your comment, but I tried it by replacing app.UIFigure with a regular figure and it worked for me (see below).
I did notice that uifigure does not appear to be supported in MATLAB online. Is that a source of your error?
clearvars
close all
% data
x1=0:.1:1;
y1=x1;
y2=100*y1;
% tiled layout
% f = uifigure; % this does not run on MATLAB online apaprently
f = figure; % use a regular figure instead
p = uipanel("Parent",f );
%t = tiledlayout(app.UIFigure,'vertical');
t = tiledlayout(p,'vertical');
%t=tiledlayout('vertical');
% first axis
ax=nexttile(t);
plot(ax, x1, y1)
ylabel(ax,'Label1 left')
hold(ax,'on')
yyaxis(ax,"right");
plot(ax,x1,y2)
ylabel(ax,'Label1 right')
% next axis
ax2=nexttile(t);
plot(ax2, x1, y2)
Kristoffer Walker
2024년 12월 11일
Strider
2024년 12월 11일
I am suspicious that the UI figure associated with the app would work. Can you try putting a panel on the app and then using the panel as the parent?
Voss
2024년 12월 11일
@Kristoffer Walker: The syntax "tiledlayout(parent)", i.e., not specifying the tiledlayout arrangement or dimensions, was introduced in R2024b (see the item here under R2024b), so that explains why
t = tiledlayout(app.UIFigure);
doesn't work for you under R2022b.
Similarly, specifying "vertical" or "horizontal" as the arrangement was introduced in R2023a, so those are also unavailable.
In R2022b, looks like your options are:
% use 'flow' arrangement
t = tiledlayout(app.UIFigure,'flow');
% or specify the dimensions
t = tiledlayout(app.UIFigure,3,2);
Kristoffer Walker
2024년 12월 12일
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

