update objects' coordinate of the same plot !!!!!!!!!!!!!!!!!!!

4 次查看(过去 30 天)
Hallo,
I am trying to plot the changing coordinate of two objects and i wasn't successful yet. So, i was wondering if someone can give me a help. To make it clear the situation look like this:
assume following two objects
classdef user1
properties(SetObservable=ture)
coordinate
end
end
classdef user2
properties(SetObservable=ture)
coordinate
end
end
classdef updateGraph
methods
function obj = updateGraph(array_obj)
addlistener(array_obj,'coordinate','PreSet',@(x,y)update(obj,src,evnt))
end
function update(obj,src,evnt)
plot(src.coordinate);
end
end
end
It doesn't work since for each call of update a new plot is been generated. How can I do to just update coordinate on the same graph?
Thanks
Bolivar

回答(1 个)

Sven
Sven 2013-8-30
Hi Bolivar,
Let's just go with one object first for simplicity:
Here's the contents of the user1 class:
classdef user1 < handle
properties(SetObservable=true)
coordinate
end
properties
coordinatePlotHandle
end
methods (Static = true)
function update(src,evnt)
obj = evnt.AffectedObject;
if isempty(obj.coordinatePlotHandle)
obj.coordinatePlotHandle = plot(obj.coordinate(:,1),obj.coordinate(:,2));
else
set(obj.coordinatePlotHandle, 'XData', obj.coordinate(:,1), 'YData', obj.coordinate(:,2))
end
end
end
end
Now you can make a user1 object:
U = user1
Now you can tell it what should happen after its coordinate is updated:
addlistener(U,'coordinate','PostSet',@user1.update);
Now you can update its coordinate(s).
U.coordinate = [1 1; 2 4]
U.coordinate = rand(5,2)
You'll notice that the first time its coordinate was set, the event was triggered and a plot was made, with the plot handle being retained and stored in U.coordinatePlotHandle. The next time the coordinate is set, that plot handle is simply updated.
Does that help answer your question?

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by