How to use 'tiledlayout' to make multiple figures in one MATLAB script.
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
Mahesh Chilla
am 29 Jun. 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 Kommentar
Biplob Chowdhury
am 29 Jun. 2023
Kategorien
Mehr zu Subplots finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



