plotting in a loop - new figure window
28 ビュー (過去 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!