Using subplot in a for loop along with a nested While loop to plot data from a large matrix
9 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
dpb
2023-1-26
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!