Plotting at set intervals in a for loop
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
NotSoWiseman
am 30 Sep. 2015
Bearbeitet: Kirby Fears
am 5 Jan. 2017
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 Kommentare
Shigeo Nakajima
am 5 Jan. 2017
If I was looking for what you stated (Kirby Fears), how would I do that?
Kirby Fears
am 5 Jan. 2017
Bearbeitet: Kirby Fears
am 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,
Akzeptierte Antwort
Kirby Fears
am 30 Sep. 2015
Bearbeitet: Kirby Fears
am 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 Kommentare
Kirby Fears
am 30 Sep. 2015
Bearbeitet: Kirby Fears
am 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!