How to pause a loop of frames using toggle button?

Hi, I have a problem with a toggle button. I want to display a loop of frames and pause it with toggle button. Displaying and pausing it is okay, but when I press a toggle button again, it shows a loop again from the start and not from the spot where it was paused.
This is my code:
button_state = get(hObject,'Value');
if button_state == 1; start = 1; while start <= nFrames
ih = image(loading(:,:,start));
colormap(gray(256));
drawnow;
pause(0);
start = start + 1;
end
elseif button_state == 0;
pause
end

 採用された回答

Rik
Rik 2018 年 1 月 1 日

0 投票

You are setting start = 1 before your loop.
What you might do is this:
while ( start <= nFrames ) && ( get(hObject,'Value') )
Or move the get inside the loop, so the loop actually continues (and unpauses when you toggle again):
while start <= nFrames
if get(hObject,'Value')
ih = image(loading(:,:,start));
colormap(gray(256));
start = start + 1;
end
drawnow;
pause(0.01);%AFAIK pause(0) has no effect
end

5 件のコメント

Jaroslava Orenicova
Jaroslava Orenicova 2018 年 1 月 1 日
I tried both but it still doesnt work.
Rik
Rik 2018 年 1 月 1 日
What didn't work? It is impossible to solve your problem if you don't explain how you implemented it and what behavior you had that you didn't want.
Or did it work after all, and you just didn't edit your comment? Is that why you accepted my answer?
Jaroslava Orenicova
Jaroslava Orenicova 2018 年 1 月 1 日
Im sorry, I didnt save my comment. I have still the same problem. Toggle button works for displaying the loop, but if I pause displaying and push button again, it shows the loop from the start and not from the spot where it was paused.
Jaroslava Orenicova
Jaroslava Orenicova 2018 年 1 月 1 日
So I have the same problem as at the beginng.
Rik
Rik 2018 年 1 月 2 日
It sounds like the structure of your callbacks isn't right. As I mentioned, this code sets start to 1 before the loop. If you plan on using this code as the callback for your button, it will overwrite each time. The solution is therefore to start the display loop only once. You can still include start=1, but I would suggest storing the value of start in the guidata. That way you can initialize start once (or each time you load a new file to you GUI). And if you're storing the value for start outside this function, why still continue the while-loop? Much better to use a break inside that while-loop and restart the loop with the loaded value.

その他の回答 (0 件)

この質問は閉じられています。

質問済み:

2018 年 1 月 1 日

閉鎖済み:

2018 年 6 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by