How to use 'tiledlayout' to make multiple figures in one MATLAB script.
조회 수: 141 (최근 30일)
이전 댓글 표시
Hello,
I am trying to use 'tiledlayout' to plot multiple plots. I tried this with 'figure' and 'subplots'. But I want to reduce the white spaces.
I want to display 6 plots in one figure, and I have 4 figures. I wrote 1 MATLAP script for all the figures using 'figure' and 'subplots'. It gives 4 figures with 6 plots in each figure.
I want to have the same results for the 'tiledlayout' as well. But in 'tiledlayout', the new figure replaces the old figure and shows only the final figure with 6 plots. I do not want to make 4 different scripts.
Can anyone please help me solve this?
Best regards
댓글 수: 0
채택된 답변
Joe Vinciguerra
2023년 6월 29일
편집: Joe Vinciguerra
2023년 6월 29일
A new figure shouldn't replace your old figure, unless you're not making a new figure first and only calling tiledlayout.
From the help file: tiledlayout(m,n) creates a tiled chart layout for displaying multiple plots in the current figure. The layout has a fixed m-by-n tile arrangement that can display up to m*n plots. If there is no figure, MATLAB® creates a figure and places the layout into it. If the current figure contains an existing axes or layout, MATLAB replaces it with a new layout.
Try this:
x=1:100;
for i = 1:4
figure
t = tiledlayout("flow");
for j = 1:6
y=i*sin(x*j);
ax = nexttile;
plot(x, y)
ylim([-6 6])
title(ax, "Tile " + j)
end
title(t, "Figure " + i)
end
추가 답변 (1개)
Mahesh Chilla
2023년 6월 29일
Hi Biplob!
To display 4 figures each having 6 subplots using "tiledlayout", you can use a nested loop, where the outer loop iterates over the number of figures specified by 'noOfFigures'. For each iteration, a new figure is created with a unique name using the 'figure' command. Inside the loop, a new tiled layout is created using the 'tiledlayout' function. The number of rows and columns for the layout is defined by the variables 'rows' and 'cols'. The 'TileSpacing' option is set to 'compact' to reduce the spacing between the subplots. The inner loop iterates over the total number of subplots. For each iteration, a new subplot is created using the 'nexttile' function. For demonstration purpose, random plot is used for all 4 figures, in case of different figures you can create a function for each case or use if else statements for the same.
The following code will display 4 figures each having 6 subplots using "tiledlayout"
% define the number of rows and columns in the tiled layout
rows = 2;
cols = 3;
noOfFigures=4; %define number of figures
for i = 1:noOfFigures
figure('Name', sprintf('Plot %d', i)); % creates a new figure for each iteration
tiledlayout(rows, cols, 'TileSpacing', 'compact'); % creates a new tiled layout for each figure
for j = 1:(rows * cols) % loop to create rows*cols plots in each figure
nexttile; % creates a new subplot
plot(rand(1, 5)); % plotting random data
title(sprintf('subplot %d', j)); % sets title for subplot
end
end
To learn more about the functions used in the code, refer the MATLAB's documentation.
Hope this helps,
Thank you!!
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!