# Using subplot in a for loop along with a nested While loop to plot data from a large matrix

15 views (last 30 days)
Elliot Cook on 26 Jan 2023
Answered: dpb on 26 Jan 2023
timeTrace = [tT1,tT2,tT3,tT4,tT5,tT6,tT7,tT8,tT9,tT10];
%For loop for plotting all our data
for i = 1:10
for k = 1:10
for j = 2 : 10
if mod(i,2) ~= 0 && mod(j,2) == 0
hold on
subplot(2,5,k)
plot(timeTrace(:,i),timeTrace(:,j))
end
end
end
end
I'm trying to use subplot to plot(timeTrace(:,1),timeTrace(:,2)), plot(timeTrace(:,3),timeTrace(:,4)), ect. but my loop is returning a subplot with plots that have overlapping data. Any help would be appreciated.
Elliot Cook on 26 Jan 2023
Also timeTrace is a 27500x20 matrix

dpb on 26 Jan 2023
Not at all clear why you're using a triply-nested loop here????
To do the requested operation on the array is simply
timeTrace = [tT1,tT2,tT3,tT4,tT5,tT6,tT7,tT8,tT9,tT10];
k=0; % counter for subplot axis
for i = 1:2:10 % loop over the number of columns by twos
k=k+1; % increment subplot
hAx(i)=subplot(3,2,k); % create the subplot axes; keep handle for later
plot(timeTrace(:,i),timeTrace(:,i+1)) % plot into this axis alternating x,y pairs
end
The new style recommended way to do this now would be to use a tiledlayout; you could either have a fixed arrangement or let it flow and rearrange itself on the fly. NOTA BENE: there are 10 traces, but only 5 plots as described; hence the 3x2 layout above is a total of six arranged for; the last is empty I figured that will be better visual plot than the alternate of 2x3; you can swap if that turns out to better suit your actual data.
hTL=tiledlayout('flow'); % a free-flow tiled layout
for i = 1:2:10 % loop over the number of columns by twos
nexttile; % create the subplot axes; keep handle for later
plot(timeTrace(:,i),timeTrace(:,i+1)) % plot into this axis alternating x,y pairs
end
will end up with the same 2x3 layout by default; again you can choose to fix the layout as desired initially.