Plotting at set intervals in a for loop

22 views (last 30 days)
I'm plotting partical postions in a for-loop with t as followed:
t = 0:timeStep:timeStep*nrOfTimeSteps
Now I think the bulk of my code is not required for this question. I want to plot the particle positions in a scatter plot at four set intervals namely [0, 1/4, 1/2, 3/4, 1] of the total time. Everything I've tried so far plots either 0 points, the particle positions at the end, or all the positions at each time step (hold on).
What is the way to implement this within my loop?
Thanks in advance
  4 Comments
Shigeo Nakajima
Shigeo Nakajima on 5 Jan 2017
If I was looking for what you stated (Kirby Fears), how would I do that?
Kirby Fears
Kirby Fears on 5 Jan 2017
Edited: Kirby Fears on 5 Jan 2017
Shigeo,
This will plot all (X,Y) position pairs for time between 0-1/4 in one plot, time between 1/4-1/2 in a second plot, etc.
T=0.1:0.1:10;
X=rand(numel(T),100);
Y=rand(numel(T),100);
stepSize=numel(T)/4;
tPoints=round(0:stepSize:numel(T));
for t = 1:(numel(tPoints) - 1),
xVals = X(tPoints(t)+1:tPoints(t+1),:);
yVals = Y(tPoints(t)+1:tPoints(t+1),:);
figure();
scatter(xVals(:),yVals(:));
end,

Sign in to comment.

Accepted Answer

Kirby Fears
Kirby Fears on 30 Sep 2015
Edited: Kirby Fears on 30 Sep 2015
Wrote a test script that scatters X and Y positions for 100 particles at 5 times as close to (0,1/4,1/2,3/4,1) portion of total time as possible. The legend shows the exact time that used for each scatter.
T=0.1:0.1:100;
X=rand(numel(T),100);
Y=rand(numel(T),100);
stepSize=(numel(T)-1)/4;
tPoints=round(1:stepSize:numel(T));
for t = 1:numel(tPoints),
scatter(X(tPoints(t),:),Y(tPoints(t),:));
if t==1,
hold on;
end,
end,
hold off;
legendTimes=num2cell(T(tPoints)');
legendTimes=cellfun(@(c)num2str(c),legendTimes,'UniformOutput',false);
legend(legendTimes);
Hope this helps.
  2 Comments
NotSoWiseman
NotSoWiseman on 30 Sep 2015
Edited: NotSoWiseman on 30 Sep 2015
Thank you for the input, I'll definitely try using the dynamic legend but I think it will be hard to implement a similar code into my script. I'm looking for something more along the lines of a simple if-statement (which I tried unsuccessfully). My loop simply plots all positions simultaneously and then replaces them as the assignment asks.
for t = 0:timeStep:timeStep*nrOfTimeSteps
a = rand(Np,1)*2*pi;
R = 2*sqrt(Dmol*timeStep);
xp = xp + vx * timeStep + R*cos(a);
yp = yp + R*sin(a);
...
If I plot within the loop and ask it to 'drawnow' I can see the movement of the particles. I simply want to grab 5 snapshots at the stated times. Something like:
hold on
if t == [0, 1/4, 1/2, 3/4, 1]
scatter(x,y)
end
Which I tried but didn't work.
Kirby Fears
Kirby Fears on 30 Sep 2015
Edited: Kirby Fears on 30 Sep 2015
In this example, you can use "hold on" after scatter(x,y) in the case that t is zero (hold after first scatter only). I used a similar technique in my answer above.
Also, try using the any() command to make your if statement work properly.
for t=0:.05:1,
if any(t == [0,.25,.5,.75,1]),
disp(num2str(t));
end,
end,
This will only fail if t is not precisely equal to the values you're comparing against, so be sure that the values do occur in the loop. If you want to allow for a margin of error in equality, you can change it to something like:
tol=1e-5;
...
if any( abs([0,.25,.5,.75,1]-t) < tol ),
...
Hope this helps.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!