# How to use animated line

19 views (last 30 days)
Kashfia Mahin on 27 Aug 2019
Commented: Kashfia Mahin on 27 Aug 2019
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 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.
h=animatedline
s = [1 2 3];
t = [2 3 4];
axis([.5,3.5,-2,14])
for i=1:length(s)
g = graph(s(i),t(i));
p= plot(g);
drawnow
end
But the figure is i don't expect this figure. And also I don't know if the structure satisfies the animation mode or not.

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);
axis([.5,3.5,-2,14]);
hold on;
for ii=1:length(s)
gplt = g;
gplt = rmedge(gplt, s(ii+1:end), t(ii+1:end));
if ii>1
delete(p);
end
p = plot(gplt, 'xdata', x, 'ydata', y, 'nodecolor', 'k', 'edgecolor', 'b');
drawnow
pause(1);
end
Kashfia Mahin on 27 Aug 2019
thank you so much
{"ft_lang_label":"__label__en","ft_lang_prob":0.82587117,"math_prob":0.9644882,"size":1137,"snap":"2021-31-2021-39","text_gpt3_token_len":319,"char_repetition_ratio":0.088261254,"word_repetition_ratio":0.0,"special_character_ratio":0.294635,"punctuation_ratio":0.20224719,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9893352,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,4,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-23T19:22:37Z\",\"WARC-Record-ID\":\"<urn:uuid:e678e8d2-2b70-4593-8885-439290b22e67>\",\"Content-Length\":\"119223\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1633a49a-6e08-4fc6-8873-ebf9def02042>\",\"WARC-Concurrent-To\":\"<urn:uuid:1da85c49-a1a1-4162-b8ec-c08ba1bd6526>\",\"WARC-IP-Address\":\"23.220.132.54\",\"WARC-Target-URI\":\"https://www.mathworks.com/matlabcentral/answers/477801-how-to-use-animated-line\",\"WARC-Payload-Digest\":\"sha1:3PAIQOU7QHF3PXD4DC3CE74O4KFMOCV5\",\"WARC-Block-Digest\":\"sha1:6RD4PLG3HUANUMZNI4HT44XNBAGGFZTP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057427.71_warc_CC-MAIN-20210923165408-20210923195408-00433.warc.gz\"}"}