How do I use tiledlayout when each plot is made within a subroutine?

3 views (last 30 days)
Most Documentation and community Answers related to tilelayout use plots formed by simple calls to MATLAB plotting routines, similar to this excerpt from the tiledlayout Documentation page:
tiledlayout(2,2);
[X,Y,Z] = peaks(20);
% Tile 1
nexttile
surf(X,Y,Z)
% Tile 2
nexttile
contour(X,Y,Z)
% ...
I want to tile several plots, each of which is made by one call to a subroutine. (Why is each made within a subroutine? because it takes tens of lines of code to accomplish what I want. See https://www.mathworks.com/matlabcentral/answers/699755-fancy-correlation-plots-in-matlab#answer_582307 for the roots of my subroutine.)
How can I use the plots created by several calls to a subroutine with tiledlayout? What exactly needs to be assigned to a variable within the subroutine and passed to the calling program? In each of the plots I want to preserve the sizes of individual points in a scatterplot, the custom colormap (the same one is used for all plots), the size and position of text, etc.
  3 Comments
Leslie
Leslie on 26 Mar 2022
To answer your question about what went wrong when I was calling nexttile() before each call to the plotting function -- things happened fast, but I appeared to get a figure prepared for tiling and then a new figure that had the actual plot in it.

Sign in to comment.

Answers (1)

Dave B
Dave B on 26 Mar 2022
You're seeing the error because when tiledlayout is in charge of positioning axes, it doesn't let you do it.
It looks to me like there's some code that's specifically setting out to set the position, but it goes against what tiledlayout is trying to do...tiledlayout is really all about trying to keep your various axes aligned. And it does this whenever your resize or change the labels or do other things that affect their position. So if you could change position yourself these two concepts would be at odds.
You're seeing the strange looking plot because there's nothing that adjusts the size of the markers based on the size of the axes. You could manually make your markers smaller looking for a size that worked well, or add (yet another) parameter to your function.
I think the short answer is:
  • remove the code setting position
  • change the size of the markers to make them smaller
  • maybe the same for the fontsize?
  • (or just make the figure window much bigger)
I experimented (very briefly) by:
Messing around with arguments till it plotted something, taking the nexttile out of the function and calling it from the outside, making the markers half as big, resizing all of the axes to have the same limits. I ended up with this (I didn't work on giving it real data or getting the colorbar to show up or messing with the text):
Longer answer:
Charts like this are part of why we built a framework for authoring charts. There's a bit of a learning curve, and I'm not sure it'll solve all of your problems, but the idea was to wrap up a bunch of parameters that determine a chart into an object...similar in some ways to some of our built-in MATLAB charts. I'm not sure it'll be worth it for you, but if you're interested you can see the doc here: https://www.mathworks.com/help/matlab/developing-chart-classes.html and some examples here: https://www.mathworks.com/matlabcentral/fileexchange?q=tag%3Achartcontainer
The idea is that you end up with an object that can take name value pairs, and that can have parameters changes after you create it. In your case an advantage would be that you could adjust the size of circles (for instance) after having all of your tiles displayed, rather than before.
Like I said, there's a learning curve, so I'm not sure it'll be worth it to you.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by