plotting in a loop - new figure window

8 visualizzazioni (ultimi 30 giorni)
Richard
Richard il 23 Feb 2012
Modificato: Matt J il 22 Ott 2013
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.

Risposta accettata

Kevin Holst
Kevin Holst il 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

Più risposte (2)

Walter Roberson
Walter Roberson il 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 Commenti
Richard
Richard il 23 Feb 2012
this doesn't produce 2 figure windows. What I want is figure(1);subplot(313) and figure(2);subplot(313) if that makes sense.
Walter Roberson
Walter Roberson il 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

Accedi per commentare.


Image Analyst
Image Analyst il 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.
  2 Commenti
Richard
Richard il 24 Feb 2012
I'm sorry, my question wasn't clear enough, its not that I need every plot to be subplot(313) I was using this as an example of the largest number of subplots per figure window (stupid I know). So, if I have 6 vectors need plotting, I want the first 3 plots to be in figure window 1 i.e. vector 1 in subplot(311), vector 2 in subplot (312), vecotr 3 in subplot (313), vector 4 in figure(2) subplot(311)... and so on. So, say that I had 11 vectors, the code should produce 3 figure windows each with 3 subplots and the fourth figure window with 2 subplots.
Richard
Richard il 24 Feb 2012
I've amended the question to show the desired outcome.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by