Easy Question, update scatter point in loop

Hi:
I just want to get the 4 vectors scatter update every time in my loop. Delete the last time points then plot then hold on to new loop.
How to do that??
Thank you.
x1{1} = [.2; .3]; x2{1} = [.3; .4]; x3{1} = [.5; .6]; x4{1} = [.6; .7]; k = 1;
for k = 1:1:50
scatter(x1{k}(1,1), x1{k}(2,1));
hold on
scatter(x2{k}(1,1), x2{k}(2,1));
hold on
scatter(x3{k}(1,1), x3{k}(2,1));
hold on
scatter(x4{k}(1,1), x4{k}(2,1));
x1{k+1} = (x1{k} + x2{k} + x4{k})/3;
x2{k+1} = (x1{k} + x2{k} + x3{k})/3;
x3{k+1} = (x2{k} + x3{k} + x4{k})/3;
x4{k+1} = (x1{k} + x3{k} + x4{k})/3;
end

 採用された回答

Ben11
Ben11 2014 年 6 月 24 日

0 投票

What if you try this:
x1{1} = [.2; .3]; x2{1} = [.3; .4]; x3{1} = [.5; .6]; x4{1} = [.6; .7]; k = 1;
figure
for k = 1:1:50
hold all
scatter(x1{k}(1,1), x1{k}(2,1))
scatter(x2{k}(1,1), x2{k}(2,1));
scatter(x3{k}(1,1), x3{k}(2,1));
scatter(x4{k}(1,1), x4{k}(2,1));
x1{k+1} = (x1{k} + x2{k} + x4{k})/3;
x2{k+1} = (x1{k} + x2{k} + x3{k})/3;
x3{k+1} = (x2{k} + x3{k} + x4{k})/3;
x4{k+1} = (x1{k} + x3{k} + x4{k})/3;
hold off
drawnow
end
First you create a figure, then you force the plot to update with drawnow. You can keep the original "hold on" calls and see what it does as well.

12 件のコメント

Yuang
Yuang 2014 年 6 月 24 日
It can not delete the previous points. I want to delete the old points and plot new points.
Ben11
Ben11 2014 年 6 月 24 日
編集済み: Ben11 2014 年 6 月 24 日
ok I'm not sure I understand then. What is the difference between old and previous points?
Yuang
Yuang 2014 年 6 月 24 日
Same. I mean I just want to plot x1(k), x2(k), x3(k), x4(k). And I update the data to x1(k+1), x2(k+1),x3(k+1),x4(k+1). To next loop, delete the last time x1,x2,x3,x4 points on graph, update plot the new 4 points.
I want to see the 4 points on one graph are moving. They should go to same place at last.
Ben11
Ben11 2014 年 6 月 24 日
ok. So is it something like this?:
x1{1} = [.2; .3]; x2{1} = [.3; .4]; x3{1} = [.5; .6]; x4{1} = [.6; .7]; k = 1;
figure
for k = 1:1:50
scatter(x1{k}(1,1), x1{k}(2,1))
hold on
scatter(x2{k}(1,1), x2{k}(2,1));
hold on
scatter(x3{k}(1,1), x3{k}(2,1));
hold on
scatter(x4{k}(1,1), x4{k}(2,1));
x1{k+1} = (x1{k} + x2{k} + x4{k})/3;
x2{k+1} = (x1{k} + x2{k} + x3{k})/3;
x3{k+1} = (x2{k} + x3{k} + x4{k})/3;
x4{k+1} = (x1{k} + x3{k} + x4{k})/3;
hold off
drawnow
end
Yuang
Yuang 2014 年 6 月 24 日
Yes. I think that's right. Can you slow down these points moving speed?
Ben11
Ben11 2014 年 6 月 24 日
You could add this line:
pause(n)
right after drawnow, where n is the time in seconds during which execution is paused.
Yuang
Yuang 2014 年 6 月 24 日
Thank you very much!!!! It help me a lot.
Ben11
Ben11 2014 年 6 月 24 日
Great! You're welcome.
Yuang
Yuang 2014 年 6 月 24 日
Sorry, one last question, do you know how to add a trial for points?
Ben11
Ben11 2014 年 6 月 24 日
Hum I'm don't know what a trial for points is sorry. Do you have an example?
Yuang
Yuang 2014 年 6 月 24 日
Sorry. I mean I want the figure shows the path x1,x2,x3 and x4 go.
Ben11
Ben11 2014 年 6 月 24 日
ok. Do you mean something like this:
for k = 1:50
scatter(x1{k}(1,1), x1{k}(2,1))
if k > 1
line([x1{k-1}(1,1) x1{k}(1,1)],[x1{k-1}(2,1) x1{k}(2,1)],'Color','r');
end
drawnow
hold on
scatter(x2{k}(1,1), x2{k}(2,1));
if k > 1
line([x2{k-1}(1,1) x2{k}(1,1)],[x2{k-1}(2,1) x2{k}(2,1)],'Color','g');
end
drawnow
hold on
scatter(x3{k}(1,1), x3{k}(2,1));
if k > 1
line([x3{k-1}(1,1) x3{k}(1,1)],[x3{k-1}(2,1) x3{k}(2,1)],'Color','b');
end
drawnow
hold on
scatter(x4{k}(1,1), x4{k}(2,1));
if k > 1
line([x4{k-1}(1,1) x4{k}(1,1)],[x4{k-1}(2,1) x4{k}(2,1)],'Color','k');
end
drawnow
hold on
x1{k+1} = (x1{k} + x2{k} + x4{k})/3;
x2{k+1} = (x1{k} + x2{k} + x3{k})/3;
x3{k+1} = (x2{k} + x3{k} + x4{k})/3;
x4{k+1} = (x1{k} + x3{k} + x4{k})/3;
hold off
pause(.1)
end
I seems odd is it what you would expect?

サインインしてコメントする。

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 6 月 24 日

0 投票

Try grabbing the handle returned from each scatter call and then deleting that on subsequent iterations. Outside the for loop declare an array to manage the scatter plot handles
% declare an array for the handles to the scatter plots
scatterHandles = [];
Now within the for loop, delete the handles if any exist
for k = 1:1:50
% delete scatter plot data from previous iteration if it exists
if ~isempty(scatterHandles)
for m=1:length(scatterHandles)
delete(scatterHandles(m));
end
end
% get the scatter plot handles for each call to scatter
hold on
scatterHandles (1) = scatter(x1{k}(1,1), x1{k}(2,1));
scatterHandles (2) = scatter(x2{k}(1,1), x2{k}(2,1));
scatterHandles (3) = scatter(x3{k}(1,1), x3{k}(2,1));
scatterHandles (4) = scatter(x4{k}(1,1), x4{k}(2,1));
% etc.
end
Try the above and see what happens!

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

質問済み:

2014 年 6 月 24 日

コメント済み:

2014 年 6 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by