problem in creating animation plot 2D

2 views (last 30 days)
I am getting huge number of figure.but not the animation plot.What is the problem in my code? Can you please help me?
%% Animation of a graph a point P(x,y) which is variable connected to three fixed point A(6,9),B(20,38),C(49,25).
clc
clear
close all
M=5000;
x=6:.01:49; % domain x.
y=9:.01:38; % domain y.
for i=1:length(x)
for j=1:length(y)
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
p = [6 20 49]; % x coordinates of three fixed points A, B, and C.
q = [9 38 25]; % y coordinates of three fixed points A, B, and C.
figure('color','white');
hold on;
for n = 1:length(p)
plot([p(n) a],[q(n) b],'color',[0 0 1]);
myChar = char(64+n);
text(p(n)+1,q(n),myChar);
end
plot(p,q,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
plot(a,b,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',8);
text(a+0.5,b+0.75,'P');
xlabel('x');
ylabel('y');
drawnow
pause = .01;
end
end
end

Accepted Answer

Bhaskar R
Bhaskar R on 12 Feb 2020
Edited: Bhaskar R on 12 Feb 2020
Put figure out side of the for loops
figure('color','white');
  2 Comments
jakaria babar
jakaria babar on 12 Feb 2020
Can you tell me please What's wrong in the code below?
clc
clear
close all
M=5000;
x=6:1:49;
y=9:1:38;
for i=1:length(x)
for j=1:length(y)
clf
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
p = [6 20 49];
q = [9 38 25];
hold on;
for n = 1:length(p)
plot([p(n) a],[q(n) b],'color',[0 0 1]);
myChar = char(64+n);
text(p(n)+1,q(n),myChar);
end
plot(p,q,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
plot(a,b,'o','markerfacecolor','r','markeredgecolor','none', ...
'markersize',8);
text(a+0.5,b+0.75,'P');
steiner = getframe;
end
end
end
mywriter = videowriter('figure');
mywriter.framerate = 20;
open(mywriter);
writevideo(mywriter, steiner)
close(mywriter)
%% command window return following:
Cannot find an exact (case-sensitive) match for 'videowriter'
The closest match is: VideoWriter in F:\Matlab Alx\toolbox\matlab\audiovideo\@VideoWriter\VideoWriter.m
Error in animate_figure (line 32)
mywriter = videowriter('figure');

Sign in to comment.

More Answers (1)

Shivaraj Durairaj
Shivaraj Durairaj on 12 Feb 2020
You must create the figure window outside the for loops.
Use hold on after every plot function except the last plot function to retain the plots for the current frame of the animation. Use hold off after the last plot function to clear the current frame and add plots to the next frame of the animation.
%% Animation of a graph a point P(x,y) which is variable connected to three fixed point A(6,9),B(20,38),C(49,25).
clc
clear
close all
M=5000;
x=6:.1:49; % domain x.
y=9:.1:38; % domain y.
figure('color','white'); % Create figure window
for i=1:length(x)
for j=1:length(y)
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
p = [6 20 49]; % x coordinates of three fixed points A, B, and C.
q = [9 38 25]; % y coordinates of three fixed points A, B, and C.
for n = 1:length(p)
plot([p(n) a],[q(n) b],'color',[0 0 1]);
hold on % hold on
myChar = char(64+n);
text(p(n)+1,q(n),myChar);
end
plot(p,q,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
hold on % hold on
plot(a,b,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',8);
hold off % hold off
text(a+0.5,b+0.75,'P');
xlabel('x');
ylabel('y');
drawnow
pause = .01;
end
end
end
  2 Comments
jakaria babar
jakaria babar on 12 Feb 2020
Edited: jakaria babar on 12 Feb 2020
Thank you so much. What is wrong the code below? Can you tell me please?
clc
clear
close all
M=5000;
x=6:1:49;
y=9:1:38;
for i=1:length(x)
for j=1:length(y)
clf
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
p = [6 20 49];
q = [9 38 25];
hold on;
for n = 1:length(p)
plot([p(n) a],[q(n) b],'color',[0 0 1]);
myChar = char(64+n);
text(p(n)+1,q(n),myChar);
end
plot(p,q,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
plot(a,b,'o','markerfacecolor','r','markeredgecolor','none', ...
'markersize',8);
text(a+0.5,b+0.75,'P');
steiner = getframe;
end
end
end
mywriter = videowriter('figure');
mywriter.framerate = 20;
open(mywriter);
writevideo(mywriter, steiner)
close(mywriter)
Command window return the following:
Cannot find an exact (case-sensitive) match for 'videowriter'
The closest match is: VideoWriter in F:\Matlab Alx\toolbox\matlab\audiovideo\@VideoWriter\VideoWriter.m
Error in animate_figure (line 32)
mywriter = videowriter('figure');
Shivaraj Durairaj
Shivaraj Durairaj on 12 Feb 2020
Edited: Shivaraj Durairaj on 12 Feb 2020
I assume that you want to create a video file from this animation. You can do that by following the steps in the Create AVI File from Animation example.

Sign in to comment.

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!