Plotting several graphs in one plot

So I've been on this problem for quite some time now, and I've looked everywhere online but I cant find it anywhere. So hopefully somebody could help me with this.
So basically, I am trying to plot particle filters through 50 time steps. I get a graph for that plot. Although, my deliemma is when I try to loop the result so that I get the graph 20 times, on the same plot.
I want to have a single plot with 20 different graphs of the same function. I dont know if thats really clear. but heres my code in very short term.
for test = 1:20
hold on
for k = 1 : 50
% True Values of State & Measurement
x = 0.5 * x + 25 * x / (1 + x^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
y = x^2 / 20 + sqrt(R) * randn;
% Particle filter
for i = 1 : 200
Xminus(i) = 0.5 * X(i) + 25 * X(i) / (1 + X(i)^2) + 8 * cos(1.2*(k-1)) + sqrt(Q) * randn;
Y = Xminus(i)^2 / 20;
Likelihood = y - Y;
q(i) = (1 / sqrt(R) / sqrt(2*pi)) * exp(-Likelihood^2 / 2 / R);
end
.
.
.
end %for the k=1:50
t = 0 : 50;
figure;
subplot (3,1,1)
plot(t, x0, 'b.', t, xPFArr, 'k-');
xlabel('Time Step');
ylabel('State');
subplot (3,1,2)
plot(t, x0, 'b.', t, xAuxArr, 'g:');
xlabel('Time Step');
ylabel('State');
subplot (3,1,3)
plot(t, x0, 'b.', t, xRegArr, 'r:');
xlabel('Time Step');
ylabel('State');
end %for the test
So each of the subplots are diffrent particle filters. I tried doing hold on before the k loop but for some reason it displays the first loop so time step to 50 of one graph. And then when it goes for the 2nd iteration my data has 100 steps instead of 50.
DOes anybody know why? And how I could fix this problem? That would be great,
fabio

 Akzeptierte Antwort

Fabio Faragalli
Fabio Faragalli am 26 Aug. 2011

0 Stimmen

oh so I found out where I made a mistake
In the test loop 1:20 I had to redefine my variables in the plotting part. Meaning xPFArr[], xAuxArr[], xRegArr[] so that I get 50 time steps each loop.
Thanks for the quick reply though

Weitere Antworten (1)

Paulo Silva
Paulo Silva am 26 Aug. 2011

0 Stimmen

Do it like this:
s1=subplot(311);hold(s1) %create each subplot and hold it
s2=subplot(312);hold(s2)
s3=subplot(313);hold(s3)
for n=1:10
axes(s1) %select the current axes
%you can avoid selecting the current axes
%plot(s1,n,1,'*') %alternative way
plot(n,1,'*')
axes(s2)
plot(n,1,'r*')
axes(s3)
plot(n,1,'g*')
end

6 Kommentare

Oleg Komarov
Oleg Komarov am 26 Aug. 2011
Did subplot always support the syntax subplot(mnp)?
Paulo Silva
Paulo Silva am 26 Aug. 2011
no idea, I've been using it since MATLAB 2007b
Oleg Komarov
Oleg Komarov am 26 Aug. 2011
Thanks.
Fangjun Jiang
Fangjun Jiang am 26 Aug. 2011
@Paulo, you are weird! Did you check every source m file before using it? I never saw subplot(mnp) before but apparently it works.
Paulo Silva
Paulo Silva am 26 Aug. 2011
No, that trick and others you learn from experienced MATLAB users (here, blogs, video tutorials, books, etc) :) and yes I'm weird :D
Walter Roberson
Walter Roberson am 26 Aug. 2011
That aspect of subplot is documented. Look at the very first of the "syntax" lines in the reference documentation:
h = subplot(m,n,p) or subplot(mnp)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by