How to produce a subplots for each iteration?

4 views (last 30 days)
Hi everyone,
My reuqire to generate plot for each iteration (either as individula or as a group 5 or 10 on each figure). For few interation, if condition not satisify the plot should be blink with iternation number so i can visulize where is something wrong.
Here is my script:
clear all
clc
% BELOW SCRIPT IS FOR MULTIPLE DAYS CALCULATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % ...........Dataset1 ............%
cd_ev=readmatrix('time.csv'); %
ev_time=datenum(cd_ev(:,1),cd_ev(:,2),cd_ev(:,3),cd_ev(:,4),cd_ev(:,5),cd_ev(:,6));
CE_M=ev_time';
for jj=1:145
b=CE_M(:,jj);
aa(jj)= addtodate(b, 5, 'day');
bb(jj)= addtodate(b, -5, 'day');
end
CE_U=aa;
CE_L=bb;
%
% % ------------ Data Set 2 -------------%
%
d_tid=readmatrix('TID.csv'); %
d_tid(1,:) = [];
d_tid(:,1) = [];
s=d_tid;
t1=datenum(s(:,1),s(:,2),s(:,3),s(:,4),s(:,5),s(:,6));
t_d=[t1 d_tid(:,7)];
%------------- Data Set 3 --------------%
d_ven=readmatrix('VENT.csv');
d_ven(1,:) = [];
d_ven(:,1) = [];
ss=d_ven;
ts=datenum(ss(:,1),ss(:,2),ss(:,3),ss(:,4),ss(:,5),ss(:,6));
v_d=[ts d_ven(:,7)];
% ---------- Data selection for a particular event ---------%
for kk=1:110
s2=t_d(t_d(:,1)>= CE_M(kk) & t_d(:,1)<= CE_U(kk),:);
s1=v_d(v_d(:,1)>= CE_M(kk) & v_d(:,1)<= CE_U(kk),:);
if length(s1) ~= length(s2)
continue;
end
S=[s1 s2];
t=S(:, 1);
t=(t-t(1));
ser1=S(:, 2)-mean(S(:,2));
ser2=S(:, 4)-mean(S(:,4));
N=length(t);
dt=(t(end)-t(1))/(N-1);
fs=1/dt; fNyq=fs/2;
f1=1; f2=4;
[z,p,k]=butter(4,[f1,f2]/fNyq,'bandpass');
[sos,g]=zp2sos(z,p,k);
y1=filtfilt(sos,g,ser1);
y2=filtfilt(sos,g,ser2);
% %--------- Zero crossing time --------%
%
zci = @(v) find(diff(sign(v))>0);
izc1=zci(y1);
izc2=zci(y2);
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZT1 = ZeroX(t(izc1),y1(izc1),t(izc1+1),y1(izc1+1));
ZT2 = ZeroX(t(izc2),y2(izc2),t(izc2+1),y2(izc2+1));
%
if length(ZT1)==length(ZT2)
tzcd=ZT1-ZT2; %zero crossing time differences
elseif length(ZT1)==length(ZT2)-1
tzcd=ZT1-ZT2(1:end-1); %zero crossing time differences
elseif length(ZT1)-1==length(ZT2)
tzcd=ZT1(2:end)-ZT2; %zero crossing time differences
end
fdom=(length(ZT2)-1)/(ZT2(end)-ZT2(1));
phz=360*fdom*tzcd; %phase lag of y1 relative to y2 (degrees)
ww=1:length(tzcd);
figure(); plot(ww,phz,'-rx');
xlabel('Time (days)'); ylabel('Lag (degrees)'); title('Phase Lag v. Time')
ylim([0,360]); set(gca,'ytick',0:90:360); grid on
end
  2 Comments
Walter Roberson
Walter Roberson on 15 Oct 2022
for kk=1:110
So you can have up to 110 sub plots? And you want to reserve space for all of those within a single figure ??
Or do you want to bundle a number of subplots per figure, such as generating a 5 x 2 array of subplots in each of 11 figures ?
Andi
Andi on 15 Oct 2022
@walter I want to plot in groups 10 per figure or 5

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 15 Oct 2022
Edited: Image Analyst on 15 Oct 2022
Try this:
for kk = 1 : 27 % 110
needNewFigure = mod(kk-1, 9) + 1;
if needNewFigure == 1
figure;
sgtitle(sprintf('Plots %d through %d', kk, kk+8));
end
% Code to generate something to plot, then...
% Show plots in a 3 by 3 grid
subplot(3, 3, needNewFigure)
drawnow;
end

More Answers (1)

Andi
Andi on 15 Oct 2022
position = 0; % position of plot in subplot
fig_num = 1; % figure() numbe
for kk=1:10
position = position + 1;
if position >= 5; position = 1; fig_num = fig_num + 1,end
figure(fig_num)
subplot(2,2,position)
plot(ww,phz,'-rx');
xlabel('Time (days)'); ylabel('Lag (degrees)');
ylim([0,360]); set(gca,'ytick',0:90:360); grid on
caption = sprintf('fig # %d, subplot # %d, kk = %d', fig_num,position, kk);
title(caption, 'FontSize', 6);
end

Tags

Products

Community Treasure Hunt

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

Start Hunting!