在循环中使用计时器

时间:2012-10-14 18:34:13

标签: matlab user-interface timer error-handling matlab-guide

我的GUI的目的是显示图像并获得用户的响应:按键(E或I)或无响应。在图像本身之间应该有3秒的暂停,显示一些文本(我的代码中为a7 UIcontrol)。问题是我需要做30次,所以我使用一个带有计时器的循环。但GUI工作得很糟糕。

应该执行以下操作:

for 30 times do
    2 sec showing text (a7)
    then showing an image for 3 sec or until I\E are pressed
end

这是我的代码;我正在添加两个版本,因为它们主要在TIMER函数和属性中有所不同。

https://docs.google.com/document/d/1N6LSDAYo_DVrBCUbuPth4JPCvkI3pBNcnAZcV6Kl9wM/edit
更具可读性的版本:http://pastebin.com/vd3HNGv1

并且照片在这里(尽管您可以使用任意2张照片):https://picasaweb.google.com/alex.goltser/ScrapbookPhotos

起初问题始终是错误:

  

你尝试在它工作时启动计时器

但现在还有别的......

1 个答案:

答案 0 :(得分:0)

为什么要运行计时器功能?

以下是另一种运行循环的方法:

for repeat = 1:30

    *show text*
    drawnow %# to make sure the graphics are updated
    pause(2) %# wait two seconds

    *show image*
    drawnow
    t = tic;
    done = false;
    while ~done && toc(t)<3 %# checks for keypress or until 3 secs
       *check for keypress*
       if E/I key has been pressed
          done = true;
       end
    end
end %# repeat 30x