plotting multiple plots in multiple figures inside a for loop

조회 수: 74 (최근 30일)
Salma fathi
Salma fathi 2023년 9월 5일
댓글: Pooja Kumari 2023년 9월 7일
Hi, I am trying to plot multiple plots using a for loop. we have 24 plots and I would like to divide them such that I would put 8 plots in each firgure, where each figure will have 4 columns and 2 rows.
I have used
tiledlayout(6,4)
to plot all of them in one figure but I am kinda stuck at how to seperate them into multiple figures.
If anyone can help. Thanks a lot in advanced.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 9월 5일
The logistics of what you want to do aren't clear to me.
Do you want to have 24 figures, with each figure having 8 plots stored in a 2x4 format?

댓글을 달려면 로그인하십시오.

답변 (2개)

Pooja Kumari
Pooja Kumari 2023년 9월 5일
편집: Pooja Kumari 2023년 9월 5일
Dear Salma,
I understand that you are facing issue with ‘tiledlayout’ function while plotting multiple graphs in multiple figures using a ‘for’ loop. You want to plot 24 plots having 8 plots in each figure, where each figure will have 4 columns and 2 rows.
For your reference, I am providing a sample solution:
[X,Y,Z] = peaks(20);
% Define the number of plots and the desired layout
numPlots = 24;
plotsPerFigure = 8;
numFigures = ceil(numPlots / plotsPerFigure);
% Loop over the number of figures
for Index = 1:numFigures
figure(Index); % We have 24 plot and each figure will have 8 plot so there are total 3 plots
% Create a new tiledlayout for each figure
t = tiledlayout(2, 4);
% Loop over the plots in the current figure
for plotIndex = 1:plotsPerFigure
% Calculate the plot index within the current figure
subplotIndex = (Index - 1) * plotsPerFigure + plotIndex;
% Select the appropriate tiledlayout for the current figure
if Index == 1
t1 = t;
elseif Index == 2
t2 = t;
elseif Index == 3
t3 = t;
end
% Select the appropriate figure and tiledlayout for the current plotIndex
if subplotIndex <= 8
figure(1);
t = t1;
elseif subplotIndex <= 16
figure(2);
t = t2;
else
figure(3);
t = t3;
end
nexttile;
plot3(X, Y, Z);
title(sprintf('Plot %d', subplotIndex));
end
end
Please refer to the below documentation for more information on tiled layout:
I hope this helps!
Regards,
Pooja Kumari
  댓글 수: 2
Salma fathi
Salma fathi 2023년 9월 7일
Thank you for your reply, however, I am having the issue that the code will plot the same excat plot in all the three figures. My code looks something like the following:
for
for
for
for
if()
plot()
else
continue;
end
end
end
end
end
And following to what you suggested I putted all the for loop that I have inside the two for loops of yours so it will look like the follwing:
[X,Y,Z] = peaks(20);
% Define the number of plots and the desired layout
numPlots = 24;
plotsPerFigure = 8;
numFigures = ceil(numPlots / plotsPerFigure);
% Loop over the number of figures
for Index = 1:numFigures
figure(Index); % We have 24 plot and each figure will have 8 plot so there are total 3 plots
% Create a new tiledlayout for each figure
t = tiledlayout(2, 4);
% Loop over the plots in the current figure
for plotIndex = 1:plotsPerFigure
% Calculate the plot index within the current figure
subplotIndex = (Index - 1) * plotsPerFigure + plotIndex;
% Select the appropriate tiledlayout for the current figure
if Index == 1
t1 = t;
elseif Index == 2
t2 = t;
elseif Index == 3
t3 = t;
end
% Select the appropriate figure and tiledlayout for the current plotIndex
if subplotIndex <= 8
figure(1);
t = t1;
elseif subplotIndex <= 16
figure(2);
t = t2;
else
figure(3);
t = t3;
end
nexttile;
title(sprintf('Plot %d', subplotIndex));
for
for
for
for
if()
plot()
else
continue;
end
end
end
end
end
end
end
I think I am doing something wrog some where but I can't seem to find the error.
Pooja Kumari
Pooja Kumari 2023년 9월 7일
Dear Salma,
You are using the nested for loop code in a wrong way.
for
for
for
for
if()
for Index = 1: numFigures
% Here the Code for 3 figure and 24 plots will come
nexttile;
plot()
else
continue;
end
end
end
end
end
By placing the nested for loops within your existing code, the plotting code will execute for each combination of loop variables. Figures and plots will be created based on the conditions you have specified.

댓글을 달려면 로그인하십시오.


Voss
Voss 2023년 9월 5일
nPlots = 24;
plotLayout = [2 4];
nPlotsPerFigure = prod(plotLayout);
nFigures = ceil(nPlots/nPlotsPerFigure);
for ii = 1:nFigures
figure
tl = tiledlayout(plotLayout(1),plotLayout(2));
tl.Title.String = sprintf('Figure %d',ii);
for jj = 1:nPlotsPerFigure
nexttile();
plot(rand(1,10))
title(sprintf('Plot %d',(ii-1)*nPlotsPerFigure+jj));
end
end

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by