How to use 'tiledlayout' to make multiple figures in one MATLAB script.

141 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Joe Vinciguerra
Joe Vinciguerra el 29 de Jun. de 2023
Editada: Joe Vinciguerra el 29 de Jun. de 2023
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 comentario
Biplob Chowdhury
Biplob Chowdhury el 29 de Jun. de 2023
Editada: Biplob Chowdhury el 29 de Jun. de 2023
Hi,
thank you for your quick response. I actually commented out the 'figure' function as I thought 'tiledlayout' would do the same, and both of them should not be used together. I uncommented the 'figure' function and it worked.
Thank you for your time.
Best regards

Iniciar sesión para comentar.

Más respuestas (1)

Mahesh Chilla
Mahesh Chilla el 29 de Jun. de 2023
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!!
  1 comentario
Biplob Chowdhury
Biplob Chowdhury el 29 de Jun. de 2023
Hi Mahesh,
Thank you for your response. All of my plots are unique, and I already had separate 'figure' functions. I used them again and it worked.
Thank you again for your time and answer.
Best regards

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by