MATLAB Answers

How to use animated line

19 views (last 30 days)
I want to use the animation in the piping network. this is a sample of a code:
s = [1 2 3];
t = [2 3 4];
G = graph(s,t);
h = plot(G);
The result of the figure is first.jpg
I want to an animation with this nework s that it can show creating edges from step by step like 1-->2, 2-->3 and so on. I build a code.
s = [1 2 3];
t = [2 3 4];
for i=1:length(s)
g = graph(s(i),t(i));
p= plot(g);
But the figure is untitled.jpg
i don't expect this figure. And also I don't know if the structure satisfies the animation mode or not.

Accepted Answer

Kelly Kearney
Kelly Kearney on 27 Aug 2019
A couple notes here...
First, plotting a graph object creates a GraphPlot object, not a line, so it can't be used (out of the box) with animatedline. But you can get the same animated effect by looping like you've done in your example (it could be more efficient to update properties of the GraphPlot rather than redraw each time, but in this particular example the extra work isn't really worth it).
Second, if you don't specify the x/y coordinates of your graph nodes, the graph command chooses them based on certain properties of the graph. And those default x/y coordinates will change depending on the edges. Assuming you don't want that, you need to manually set the coordinates.
Finally, be careful how you're building your graphs. You want to preserve all the nodes, not just the ones with edges, I presume.
s = [1 2 3];
t = [2 3 4];
nnode = max([s t]);
x = ones(1,nnode);
y = 1:nnode;
g = graph(s,t);
hold on;
for ii=1:length(s)
gplt = g;
gplt = rmedge(gplt, s(ii+1:end), t(ii+1:end));
if ii>1
p = plot(gplt, 'xdata', x, 'ydata', y, 'nodecolor', 'k', 'edgecolor', 'b');
  1 Comment
Kashfia Mahin
Kashfia Mahin on 27 Aug 2019
thank you so much

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!