plotting in a loop - new figure window
15 次查看(过去 30 天)
显示 更早的评论
The following example produces a subplot of the 3 variables below (located in a structure):
clear all
Data.S1 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S2 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S3 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
% Data.S4 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
% Data.S5 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
% Data.S6 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
a = fieldnames(Data);
cmap = hsv(length(a));
for i=1:length(fieldnames(Data));
subplot(3,1,i)
plot(Data.(a{i}).data1,'color',cmap(i,:));
end
If I were to uncomment the last three lines of 'Data' hence have 6 variables in total, how would I alter the loop to produce subplots of all of the data. Keeping in mind that the number of subplots in each figure should not exceed 3 (plots get too small). So, from this example I should have 2 figure windows with 3 subplots in each. How would I go about doing this?
Amended:
clear all
Data.S1 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S2 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S3 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S4 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S5 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S6 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
a = fieldnames(Data);
figure(1)
for i=1:3;
subplot(3,1,i);
plot(Data.(a{i}).data1);
end
figure(2)
for i=1:3
for ii=3:6;
subplot(3,1,i);
plot(Data.(a{ii}).data1);
end
end
This is the outcome I need.
0 个评论
采纳的回答
Kevin Holst
2012-3-2
how about:
clear all
Data.S1 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S2 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S3 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S4 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S5 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S6 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
Data.S7 = struct('data1',rand(12,1),'data2',rand(12,1),'data3',rand(12,1));
a = fieldnames(Data);
numPlots = length(a);
numFigs = ceil(numPlots/3);
for i = 1:numFigs
figure
for j = 1:3
plotNum = j + 3*(i-1);
if plotNum <= numPlots
subplot(3,1,j)
plot(Data.(a{plotNum}).data1)
end
end
end
0 个评论
更多回答(2 个)
Walter Roberson
2012-2-23
pwide = 3; %max subplots wide
nfields = length(a);
pslots = pwide * ceil(nfields / pwide);
for i - 1 : nfields
subplot(pslots, ceil(i / pwide), 1 + mod(i-1, pwide))
plot(Data.(a{i}).data1,'color',cmap(i,:));
end
2 个评论
Walter Roberson
2012-2-23
pwide = 3; %max subplots wide
nfields = length(a);
for i - 1 : nfields
if mod(i,pwide) == 1; figure; end
subplot(pwide, ceil(i / pwide), 1 + mod(i-1, pwide))
plot(Data.(a{i}).data1,'color',cmap(i,:));
end
Image Analyst
2012-2-23
So just have it do this:
for i=1:length(fieldnames(Data));
figure;
subplot(3,1,3); % Note 3,1,3, not 3,1,i like before.
plot(Data.(a{i}).data1,'color',cmap(i,:));
end
That will give you a new figure each iteration and put your image in the bottom third of the figure like you asked for in your comment to Walter.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!