# problem in creating animation plot 2D

조회 수: 7(최근 30일)
jakaria babar 2020년 2월 12일
편집: Shivaraj Durairaj 2020년 2월 12일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

편집: Bhaskar R 2020년 2월 12일
Put figure out side of the for loops
figure('color','white');
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
jakaria babar 2020년 2월 12일
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');

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Shivaraj Durairaj 2020년 2월 12일
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표시숨기기 이전 댓글 수: 1
Shivaraj Durairaj 2020년 2월 12일
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.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by