I need some input from the keyboard while displaying a plot. With the waitforbuttonpress()
function I can detect whether the mouse button or a key on the keyboard has been pressed, but I cannot get the specific key/character selected.
I need a method which works from the plot output window, not from the text console.
This is what I have so far:
clf;
colormap ("default");
filename_orig = "./orig.data"
load("-text", filename_orig, "M")
M
t = 1
h = imagesc (M(:,:,t));
title ("Test data");
xlabel ("x");
ylabel ("y");
# Loop across time
b = 0;
for i = 1:10
for t = 1:size(M,3)
title(["Test data; t = ", num2str(t)]);
set(h, 'cdata', M(:,:,t)) # update latest frame
pause(0.20) # keep >0 to ensure redraw
b = waitforbuttonpress();
if(b == 1)
break;
endif
end
if(b == 1)
break;
endif
end
I would like to remove the outer loop, and allow to increment/decrement the t
variable according to the user's key presses.
By the way, is it possible to remove that double break
to exit from a nested loop?
Ideally, this is what I would like:
# Loop across time
b = 0;
t = 0;
while(1)
title(["Test data; t = ", num2str(t)]);
set(h, 'cdata', M(:,:,t)) # update latest frame
pause(0.20) # keep >0 to ensure redraw
k = readKeyboard(); # pseudo function
if(k == '+')
t = t + 1;
endif
if(k == '-')
t = t - 1;
endif
if(t < 0)
t = max;
endif
if(t > max)
t = 0;
endif
end
You can attach a WindowKeyPressFcn callback function to the figure as:
global t
function my_cbf(object, event)
k = event.key;
if(k == '+')
t = t + 1;
end
...
end
figure_handle = gcf();
set(figure_handle, 'WindowKeyPressFcn', my_cbf)