MATLAB: For loop forward and backward

for loopfor loop forward and backward

How to make the loop below count increasingly (1,2,3,…) when I press the mouse button and count decreasingly (4,3,2,…) when if press a keyboard button?
for i = 1:10
disp( num2str( i ) );
w = waitforbuttonpress;
end

Best Answer

I'm not sure if it can be done in a for loop, but in a while loop:
i = 1;
while i >= 1 && i <= 10
disp( num2str( i ) );
if waitforbuttonpress
i = i - 1;
else
i = i + 1;
end
end