用“如果”语句暂停循环?

13次观看(最近30天)
MSolano
MSolano on 31 Aug 2021
评论: MSolano在2021年9月2日
Hello everyone
我正在使用应用程序设计器来制作一段时间循环的动画,我希望当时间达到值时,我希望动画暂停(假设5),我使用“ if”语句来执行此操作,因此我有以下代码
The problem is that when I say: if (toc(t_h)-app.PauseTime) == 5 it does nothing (the animation doesn't stop)
but if I write: if (toc(t_h)-app.PauseTime) > 5 instead it does work but I don´t want it to stop in 5.01 or 4.99 I need it to stop in the exact 5
你们中的任何人都可以给我一个线索,我真的很感激。谢谢!
属性(Access = public)
PauseTime = 0;% Description
暂停= tic;
结尾
处理组件事件的%回调
方法(访问=私有)
% Button pushed function: StartButton
functionStartButtonPushed(应用程序,活动)
all
set(app.startbutton,'Text',,,,'重新开始'
时间= 60;
set(app.UIAxes,'units',,,,“点”
CLA(app.uiaxes);
CLA(app.uiaxes2);
r = 65;
ang = linspace(0,2*pi,50);
xp = r*cos(ang);
yp=R*sin(ang);
%的动画
%绘制自大圈子
补丁(app.uiaxes,xp,yp,'W'
hold(app.uiaxes,'上');
hold(app.uiaxes2,'上');
轴(app.uiaxes,'equal');
F = 0.016;
NB = 10;
% Balls
rho1 = 55;
n = linspace(0,360,Nb+1);
line = plot(app.uiaxes,nan,nan,'o',,,,'行宽',1,'MarkerSize',15,'颜色',,,,'k');
% 标记on 1st ball
妈rk = plot(app.UIAxes,NaN,NaN,'*',,,,'行宽',1,'MarkerSize',5,'颜色',,,,'r');
固定分数%
plot(app.UIAxes,[55 65],[0 0],'行宽',5,'颜色',,,,'r');
%正弦波
% 静止的
swx = linspace(0,时间,时间*100);
swy =(pi/2)*sind(360*swx*f);
情节(app.uiaxes2,swx,swy,'行宽',1,'MarkerSize',1,'颜色',,,,'k');
百分比运动点正弦波
sw = plot(app.uiaxes2,nan,nan,'o',,,,'行宽',1,'MarkerSize',5,'颜色',,,,'r',,,,“ MarkerfaceColor”,,,,'r');
t_h = tic;
app.pausetime = 0;
while(toc(t_h)-app.pausetime)
% Balls
t1 = toc(t_h)-app.pauseTime;
F1 = F*360;
line.xdata = rho1*cosd(n+f1*t1);
line.ydata = rho1*sind(n+f1*t1);
app.time.value = t1;
正弦波上的%点
t2=toc(t_h)-app.PauseTime;
sw.XData = t2;
sw.ydata =(pi/2)*sind(360*f*t2);
%滑块
t3 = toc(t_h)-app.pauseTime;
app.slider.value = t3;
% 标记
t4=toc(t_h)-app.PauseTime;
F1 = F*360;
妈rk.XData = rho1*cosd(f1*t4);
妈rk.YData = rho1*sind(f1*t4);
如果(toc(t_h)-app.pausetime)== 5
papebuttonpushed(app,matlab.ui.eventdata.buttonpusheddata)
结尾
绘制
结尾
结尾
% Button pushed function: PauseButton
functionpapebuttonpushed(应用,活动)
app.pausehandle = tic;
UIWAIT(App.Animation)
结尾
% Button pushed function: ResumeButton
functionremumebuttonpushed(应用,活动)
app.pausetime = app.pausetime + toc(app.papehandle);
uiresume(app.animation)
结尾
1条评论
Fangjun Jiang
Fangjun Jiang on 31 Aug 2021
浮点数据平等比较问题?
1-1/3==2/3

登录发表评论。

接受的答案

cr
cr on 1 Sep 2021
You must give it a range of time period rather than a unique time point since the execution doesn't guarantee the particular line toc() will be executed at t = 5s. If you need it to stop only once try giving a shorter period.

更多答案(0)

s manbetx 845


Release

R2021A

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

Start Hunting!