# How to manage multiple subplots ?

33 visualizzazioni (ultimi 30 giorni)
Andi il 9 Dic 2021
Commentato: Andi il 9 Dic 2021
My script run
for 1=1:185
for every iteration, it generate two subplots. However, when I try to plot them in combination it shows either first subplot or secodn only:
The detail is as follow:
clear all
clc
ev_hr=ev_hr';
bbb=ev_bg; % rate calculation
position = 0; % position of plot in subplot
fig_num = 1; % figure() number
for S=1:10
uu=S;
r=bbb(uu); % for first event
data=ev_hr(:,uu);
u=transpose(data);
for n=1:length(u);
dt=n;
for i=1:length(u)-(n-1);
K=u(i:i+n-1);
k=sum(K);
b(i,n)=(k-(r)*(dt))/sqrt(r*dt);
end
end
T=transpose(b);
for ii=1:72
for jj=1:72-(ii-1)
if(ii==1)
b1(ii,jj)=T(ii,jj);
else
b1(ii,jj+(ii-1))=T(ii,jj);
end
end
end
for ii=1:length(b)
for jj=1:72
if(b1(ii,jj)==0)
b1(ii,jj)=nan;
end
end
end
c = NaN(72,24);
c2=[c b1];
x = [-23:72]' ;
y = [1:72]' ;
position = position + 1;
if position > 10; position = 1; fig_num = fig_num + 1,end
figure(fig_num)
%--------------Part A (subplot 1) ...............%
subplot(5,2,position);
pcolor(x,y,c2);
colorbar
%---------------- Part B (subplot 2)------------%
subplot(5,2,position);
A=A';
uuu=A(:,uu);
ss=sum(uuu);
ss=ss+20;
bb=cumsum(uuu);
h=[-23:72];
plot(h,bb, 'b')
axis([-23 72 0 ss])
saveas(gcf,'ev_01.png')
end
Case 1: If I run this script or (comment the part A), the output only show the subplot 2
Case 2: if I comment the part B the output only shows the subplot 1
What I need?
I need the combined output: First column should be subplot 1 and second should be subplot 2 and so on.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Awais Saeed il 9 Dic 2021
I hope this will work for you.
clear all
position = 0; % position of plot in subplot
fig_num = 1; % figure() number
for k = 20:30
x = rand(1,1).*linspace(0,2*pi,k);
y = sin(x).*cos(x);
xcod = ceil(10.*rand(3,3));
ycod = xcod';
Cmat = ceil(10.*rand(3,3));
position = position + 1;
% if position >= 11, create a new figure window and reset position value
if position >= 11; position = 1; fig_num = fig_num + 1,end
figure(fig_num)
% caption for title for each figure
caption = sprintf('Fig # %d, starting k = %d', fig_num, k);
subplot(5,2,position)
plot(x,y)
position = position + 1;
subplot(5,2,position);
pcolor(xcod,ycod,Cmat);
% main title (starting from R2018b)
sgtitle(caption);
end
fig_num = 2
fig_num = 3
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Andi il 9 Dic 2021
Thank you very much. I got the required results.

Accedi per commentare.

### Più risposte (1)

Walter Roberson il 9 Dic 2021
subplot(5,2,position);
You do that same subplot() twice, but you do not change position between the two calls.
Start your position at 1, and increment it by 2 each time, and the first subplot goes at 5,2,position and the second one to the right of it goes at 5,2,position+1
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Andi il 9 Dic 2021
Even, if i change the position the situation remain same.

Accedi per commentare.

### Categorie

Scopri di più su Subplots 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!

Translated by