MATLAB Answers

Issue with legends/fonts and tiledlayout

10 views (last 30 days)
Allen Parker
Allen Parker on 16 Feb 2021
Commented: Allen Parker on 19 Feb 2021
Hello, I've recenty discovered the tiledlayout() function, and it's working swimingly, with one strangely specific issue. Whenever I set the 'FontUnits' to 'normalized', I'm unable to display a legend properly. Doing everything the same with any other FontUnits, or keeping the normalized FontUnits and not showing a legend, both fix the problem.
This code demonstrates my issue (using R2020b):
%%
clc,clear
x=0:.25:30;
for figNum=1:3
figure(figNum),clf
if figNum<=2,set(figNum,'defaultAxesFontUnits','normalized','defaultAxesFontSize',.04),end
tiledlayout(1, 2, 'Padding', 'none', 'TileSpacing', 'compact');
nexttile,plot(x,sin(x),'DisplayName','Sin(x)')
if figNum>=2,legend show,end
nexttile,plot(x,cos(x),'DisplayName','Cos(x)')
if figNum>=2,legend show,end
end

Answers (1)

Nikhil Sonavane
Nikhil Sonavane on 19 Feb 2021
According to my understanding you are trying to mix up two different things, figure and tiledlayout are two different approches to view graphs, you don't need to use both of them. You may refer to the documentation page of tiledlayout to understand it better. Also, the syntax that you are using for legend is also incorrect. You may refer to documentation of legend as well.
  1 Comment
Allen Parker
Allen Parker on 19 Feb 2021
Thanks for your response. Please let me know if I'm incorrect, but I don't believe either of those 1) is an issue or 2) addresses the problem.
From the documentation of tiledlayout: "tiledlayout(m,n) creates a tiled chart layout for displaying multiple plots in the current figure... If there is no figure, MATLAB® creates a figure and places the layout into it. If the current figure contains an existing axes or layout, MATLAB replaces it with a new layout." So while it's correct that I don't need to call figure() to use tiledlayout(), I do need to instantiate a new figure via figure() whenever one is needed, or every new tiledlayout() will be overlaid in the current one. I wrote the example as I did to demonstrate both what works and what doesn't, so it does need a new figure for every loop iteration. If you run the example, you'll see three figures appear, as intended.
legend('show') may be preferred syntax to "legend show", but making that update also doesn't resolve the issue.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by