MATLAB: How to create triangle and animate it

animationtriangle

I'm using this code to start but hasnt worked. patch command is giving error and not sure how to resolve it. Please help.
w0 = 1 ;
w1 = 0.5 ;
N = 5 ;
ww = linspace(w1,w0,N) ;
W = [fliplr(ww) ww] ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*W;%height of triangle
for i = 1:length(W)
x=[0 W W/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
patch(x,y,'white')
set(gca,'Color','k','xticklabel',[],'yticklabel',[])
drawnow
pause(0.1)
end

Best Answer

Try this
w0 = 1 ;
w1 = 0.5 ;
N = 5 ;
ww = linspace(w1,w0,N) ;
W = [fliplr(ww) ww] ;
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*W;%height of triangle
fig = figure();
ax = axes();
set(ax,'Color','k','xticklabel',[],'yticklabel',[])
ax.XLim = [0 1];
ax.YLim = [0 1];
p = patch([0 0 0], [0 0 0], 'w');
for i = 1:length(W)
x = [0 W(i) W(i)/2];%x coordinates of vertices
y = [0 0 h(i)];%y coordinates of vertices
p.Vertices = [x.' y.'];
drawnow
pause(0.1)
end