Plotting at set intervals in a for loop

25 次查看(过去 30 天)
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 个评论
Shigeo Nakajima
Shigeo Nakajima 2017-1-5
If I was looking for what you stated (Kirby Fears), how would I do that?
Kirby Fears
Kirby Fears 2017-1-5
编辑:Kirby Fears 2017-1-5
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,

请先登录,再进行评论。

采纳的回答

Kirby Fears
Kirby Fears 2015-9-30
编辑:Kirby Fears 2015-9-30
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 个评论
NotSoWiseman
NotSoWiseman 2015-9-30
编辑:NotSoWiseman 2015-9-30
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 2015-9-30
编辑:Kirby Fears 2015-9-30
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by