我有一个正方形,它根据列向量x(在底部(沿着x轴来回移动。我想为正方形沿着x轴来回滑动的运动设置动画。我尝试在for循环中使用patch、hold on、hold off和Get-Frame函数,但没有成功。相反,结果是正方形被涂抹,因为旧的正方形没有从图中删除
clf
s = .1;
figure(1);
axis equal;
axis([-1 1 -1 1]);
for i = 1:length(x)
hold on
X = [x(i,1) - .5*s;
x(i,1) + .5*s;
x(i,1) + .5*s;
x(i,1) - .5*s;];
Y = [-.5*s;
-.5*s;
+.5*s;
+.5*s];
patch(X,Y,'k');
hold off;
h = gca;
F(i) = getframe;
end
movie(F);
这是列向量x:
0.25亿0.2307821591435420.1762800435366890.09531468567981280.000650212865865713-0.0932030485077490-0.172186726125664-0.224543041359108-0.242303458811820-0.222203182817347-0.16619208585484-0.08175077923202680.01862987670498160.1181709916530330.1989055401015430.2460012608760510.2509590241740340.2131077244165970.1388227335331250.0407394702062158-0.0642948796656877-0.158085053404849-0.224759025665608-0.253100286376047-0.238297378581389-0.182618266275528-0.09588969047133700.006028395221970840.1046685111353330.1837529543070200.2315749996187350.2421278120518110.2146325386428150.1538562033375480.0689710768423088-0.0273378670448299-0.120371418450032-0.195696352800403-0.241063326708591-0.249170206446669-0.218574858836030-0.154523974516009-0.06746607343094350.02888907827511090.1198133308281380.1918776269287920.2343642706720300.2409411679813510.2098933796675720.1451307177266660.0553977698549078-0.0457864898288502-0.141584098341353-0.2114945647396668-0.252189311075765-0.246607701950267-0.199000344173733-0.117539948853725-0.01635328989535760.08706954064691150.1751273256246040.2334302144638700.2524109457541330.2288050227554300.1658689161654040.0741157391779560-0.02998252244149044-0.127575941311610-0.201938733706939-0.241944848784945-0.242264519853537-0.203786790215477-0.132560148710489-0.03953940717969790.06088717760368580.1526366292982160.2202956401891070.2524420544881790.2434462023650580.1953228810133230.1166478689877910.0209866982124944-0.0762128632358914-0.160101037237629-0.218549398091714-0.243311095720934-0.230961374436814-0.183209095455776-0.106644747551930-0.01231091180424670.08572901983451980.1719006209890600.2315414843781190.2541920564756180.2353711650490590.1783523358912240.0928137722294060-0.00672172862573606-0.10403753998919-0.183858736347838
我最终放弃了补丁函数,因为它似乎不适用于hold-on-hold-off命令。我用plot函数画了四条线,如下所示。
clc
clf
s = .25;
min = -2;
max = 2;
for i = 1:length(x)
X = [x(i,1)-.5*s, x(i,1)+.5*s];
Y = [-.5*s, -.5*s];
plot(X,Y,'b');
axis equal
axis([min max min max]);
hold on;
X = [x(i,1)+.5*s, x(i,1)+.5*s];
Y = [-.5*s, .5*s];
plot(X,Y,'b');
axis equal
axis([min max min max]);
hold on;
X = [x(i,1)+.5*s, x(i,1)-.5*s];
Y = [.5*s, .5*s];
plot(X,Y,'b');
axis equal;
axis([min max min max]);
hold on;
X = [x(i,1)-.5*s, x(i,1)-.5*s];
Y = [.5*s, -.5*s];
plot(X,Y,'b');
axis equal;
axis([min max min max]);
hold off
F(i) = getframe;
end
movie(F)