stock plots, errorbars in a growing variable.

1 view (last 30 days)
I find a little bit difficult to understand how to propperly handle multiple figures in matlab.
I am creating different plots (set of points (of vector values) and errorbars) that are created inside a loop and stacking them together as the loop is runned (using hold on).something like this:
for i=1:5
for j=1:2
figure (1)
and wanted to create a subplot of the combination of them. I know that I can use the subplot directly inside of the loop to stack them directly. but what I was wondering is to be able to define the figures as a "single" figure and then calling it back to use the tiledlayout .Something like stacking them inside a variable array that could be called as fig(1) , fig(2) etc.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 23 Sep 2021
I'm not sure I completely follow your question, but I think you are asking about using handles to graphics objects. For example, when you call figure, instead of passing in "1" or "2" (which is the figure numeric identifier), you can just call the figure command and store the output. Similarly, you can call the axes or nexttile commands to create axes, and then store the output to use as a handle to the axes. This will let you add new things to existing axes. Then, when you call plot or errorbar, provide the axes handle as the first input so the command knows which axes to use when plotting.
Here is a code example:
f1 = figure; % Create a new figure.
ax1 = axes(f1); % Create an axes in the first figure.
hold(ax1, 'on');
f2 = figure; % Create a new figure.
ax2 = axes(f2); % Create an axes in the second figure.
hold(ax2, 'on');
for i=1:5
for j=1:2
plot(ax1, (i+j):(i+j+4), j:(j+4))
errorbar(ax1, (i+j+1):(i+j+5), j:(j+4))
plot(ax2, (i+j):(i+j+4), j:(j+4))
errorbar(ax2, (i+j+1):(i+j+5), j:(j+4))
If you want the two axes in a single figure, I recommend using nexttile to create the axes:
f = figure; % Create a new figure.
t = tiledlayout('flow');
ax1 = nexttile(t);
ax2 = nexttile(t);
hold(ax1, 'on');
hold(ax2, 'on');
for i=1:5
for j=1:2
plot(ax1, (i+j):(i+j+4), j:(j+4))
errorbar(ax1, (i+j+1):(i+j+5), j:(j+4))
plot(ax2, (i+j):(i+j+4), j:(j+4))
errorbar(ax2, (i+j+1):(i+j+5), j:(j+4))
franco otaola
franco otaola on 23 Sep 2021
Thanks a lot! now i understand a little bit better, what I was tying to do, was a combination of the two codes you gently posted. have two plots one that have two axis (and not two subplots) in the same figure. I adapted from your script if anyone comes across this post. in any case, thanks a lot!
EDIT: also for what concerned my initial quesiton we can also stock multiple axis inside a variable
f1 = figure;
ax1 = axes(f1);
plot(ax1, 1:10)
f2 = figure;
ax2 = axes(f2);
plot(ax2, sin(1:10))
savefig(f2, 'fig2')
% I'll put two axes into this figure, just to show how that might change
% things.
f3 = figure;
plot(ax, 1:10)
plot(ax, 1:20,'*')
savefig(f3, 'fig3')
% Now create a new figure to contain the contents of the other figures.
fnew = figure;
% Put a tiledlayout into the figure to hold all the axes from the other
% figures.
tnew = tiledlayout(fnew, 'flow');
% Create a list of fig files to load.
figFiles = ["fig1", "fig2", "fig3"];
numAxes = 0;
for f = 1:numel(figFiles)
% Load the fig file
loadedFig = openfig(figFiles(f));
% Find all the axes in the figure.
ax = findobj(loadedFig, 'Type', 'axes');
% Move the axes into the new tiledlayout.
for a = 1:numel(ax)
numAxes = numAxes + 1;
% Set the parent of the axes to be the new tiled layout, which will
% move the axes into that new figure.
ax(a).Parent = tnew;
% Set the Layout.Tile to a unique number so that each axes will not
% overlap other axes.
ax(a).Layout.Tile = numAxes;
% Now close the empty loaded figure.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by