plotting in a loop - new figure window
28 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Kevin Holst
on 2 Mar 2012
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 Comments
More Answers (2)
Walter Roberson
on 23 Feb 2012
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 Comments
Walter Roberson
on 23 Feb 2012
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
on 23 Feb 2012
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.
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!