How to plot a tiled layout using a loop?
149 ビュー (過去 30 日間)
古いコメントを表示
Hi guys, I have bunch of excel files that almost everything in them is similar. All I want is creating a loop instead of coding 10 figures, that some data is loaded from row (I can get this part done), then using a tiled layout or subplot to click run and MATLAB loops and adds figures to the next tile but I cant get this going:
for r = 1:10
X=xlsread(B) - for instance
tiledlayout(2,5)
scatter(X(the plot works))
nexttile
end
0 件のコメント
採用された回答
ANKUR KUMAR
2021 年 3 月 12 日
編集済み: ANKUR KUMAR
2021 年 3 月 12 日
for r = 1:10
subplot(2,5,r)
plot(randi(10,5,5),'ro')
end
If you have two loops, you can use as follows:
for r = 1:2
for k=1:5
subplot(2,5,(r-1)*5+k) % 5 is used because the maximum iteration of k is 5
plot(randi(10,5,5),'ro')
end
end
11 件のコメント
ANKUR KUMAR
2021 年 3 月 12 日
We need to use cat command to concatenate multiple separate matrices into a single matrix. The first argument in the cat command is the dimension along which you wish to concatenate.
その他の回答 (2 件)
Adam Danz
2021 年 3 月 12 日
編集済み: Adam Danz
2021 年 3 月 12 日
> should a nexttile be in the loop?
Yes, but the tiled layout should be defined before the loop.
The first two examples listed in this answer show how to use tiledlayout in a loop with a global legend.
Here's another example.
fig = figure();
tlo = tiledlayout(2,3);
h = gobjects(1,6);
colors = lines(6);
for i = 1:6
ax = nexttile(tlo);
h(i) = plot(ax, 1:10, rand(1,10), 'Color', colors(i,:), 'DisplayName', sprintf('line %d',i));
end
lg = legend(h);
lg.Layout.Tile = 'East'; % <-- place legend east of tiles
Wolfgang McCormack
2021 年 3 月 12 日
2 件のコメント
Danish Ali
2022 年 3 月 8 日
Hey Adam ! @Adam Danz
I have similar question;
How can we plot multiple figures with subplots using loop?
For example, using loops, MATLAB should generate 17 figures and allocate 10 subplots in each figure.
Figure should have 1 to 10 subplots then figure two should have 11 to 20 and so on.
There are 170 samples. Any idea how it can be done ?
I would appreciate your help.
Adam Danz
2022 年 3 月 9 日
編集済み: Adam Danz
2022 年 3 月 9 日
nFigures = 17; % number of figs
subplotGrid = [5,5]; % subplot grid arrangement
nAxes = prod(subplotGrid);
figHandles = gobjects(1,nFigures);
axesHandles = gobjects(nFigures, nAxes);
for i = 1:nFigures
figHandles(i) = figure();
for j = 1:nAxes
axesHandles(i,j) = subplot(subplotGrid(1),subplotGrid(2),j,figHandles(i));
end
end
Not tested
参考
カテゴリ
Help Center および File Exchange で Subplots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!