- https://www.mathworks.com/help/matlab/ref/tiledlayout.html
- https://www.mathworks.com/help/matlab/ref/nexttile.html
plot in designated subfigures in loops
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
feynman feynman
il 19 Mag 2024
Risposto: Abhishek Kumar Singh
il 19 Mag 2024
I have the following code. I want to plotsubfigures as required in the comments in main.m. How to plot in designated subfigures in such loops below?
% main.m
tiledlayout(2,3)
for i=1:2
for j=1:3
function myfun;
end
end
% myfun.m
plot % in subplot(1,j)
plot % in subplot(2,j)
0 Commenti
Risposta accettata
Abhishek Kumar Singh
il 19 Mag 2024
You can use tiledlayout and nexttile to create a tiled chart for multiple layouts.
Refer to the documentation for more information and assiciated examples at:
For your case, I tried with a dummy myfun function as:
function myfun(i, j)
% Generate a sine wave with varying frequency and amplitude
x = linspace(0, 2*pi, 100);
frequency = i; % Frequency increases with i
amplitude = j; % Amplitude increases with j
y = amplitude * sin(frequency * x);
% Calculate the tile index based on i and j for a 2x3 layout
tileIndex = (j-1) + (i-1)*3 + 1;
% Plot in the appropriate subplot
nexttile(tileIndex);
plot(x, y); % Plot the sine wave
% Customize the plot
title(sprintf('Freq: %d, Amp: %d', frequency, amplitude));
xlabel('x');
ylabel('sin(x)');
end
Now using the main.m script, as you are suggesting, you can plot in designated 'subfigures':
tiledlayout(2,3) % Create a 2x3 grid of tiles
for i=1:2
for j=1:3
myfun(i, j); % Call myfun with row (i) and column (j) indices
end
end
This demonstrates how to use nested loops and the tiledlayout with nexttile to create complex subplot arrangements.
Hope it helps!
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!