stock plots, errorbars in a growing variable.

2 visualizaciones (últimos 30 días)
franco otaola
franco otaola el 23 de Sept. de 2021
Editada: franco otaola el 23 de Sept. de 2021
Hello,
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)
plot(a{i}{j}(:,1),b{i}{j}(:,1))
errorbars(a{i}{j}(:,1),b{i}{j}(:,1),Uuper{i}{j}(:,1),Ulower{i}{j}(:,1))
figure(2)
plot(a{i}{j}(:,1),c{i}{j}(:,1))
errorbars(a{i}{j}(:,1),c{i}{j}(:,1),Uuperc{i}{j}(:,1),Ulowerc{i}{j}(:,1))
end
end
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.
thanks

Respuesta aceptada

Benjamin Kraus
Benjamin Kraus el 23 de Sept. de 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))
end
end
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))
end
end
  3 comentarios
Benjamin Kraus
Benjamin Kraus el 23 de Sept. de 2021
Editada: Benjamin Kraus el 23 de Sept. de 2021
Handling yyaxis with this approach is pretty straightforward. For example:
ax = axes;
yyaxis(ax,'left')
plot(ax, 1:10)
yyaxis(ax,'right')
errorbar(ax, 11:20, 1:10, 0.1:0.1:1)
As for moving axes from one figure to another, that is a bit tricker. One thing to note is the distinction between a figure (which represents a window that contains some graphics) vs. an axes (which is one set of x-ruler + y-ruler + ticks + the data inside). What I suspect you want to do is to take the axes that are within a saved figure, and move them into another new figure. Here is one way to do that using tiledlayout.
f1 = figure;
ax1 = axes(f1);
plot(ax1, 1:10)
savefig(f1,'fig1')
close(f1)
f2 = figure;
ax2 = axes(f2);
plot(ax2, sin(1:10))
savefig(f2, 'fig2')
close(f2)
% I'll put two axes into this figure, just to show how that might change
% things.
f3 = figure;
t = tiledlayout(f3,'flow');
ax3 = nexttile(t);
plot(ax3, 11:20)
ax4 = nexttile(t);
plot(ax4, cos(1:10))
savefig(f3, 'fig3')
close(f3)
% 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;
end
% Now close the empty loaded figure.
close(loadedFig);
end
franco otaola
franco otaola el 23 de Sept. de 2021
Editada: franco otaola el 23 de Sept. de 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)
savefig(f1,'fig1')
close(f1)
f2 = figure;
ax2 = axes(f2);
plot(ax2, sin(1:10))
savefig(f2, 'fig2')
close(f2)
% I'll put two axes into this figure, just to show how that might change
% things.
f3 = figure;
ax=axes(f3)
yyaxis(ax,'left')
plot(ax, 1:10)
yyaxis(ax,'right')
plot(ax, 1:20,'*')
savefig(f3, 'fig3')
close(f3)
% 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;
end
% Now close the empty loaded figure.
close(loadedFig);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Object Properties en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by