Reputation: 618
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
I am wondering, 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. I was thinking of using some kind of if statement, but would appreciate some advice on the matter.
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.
Upvotes: 0
Views: 3175