I have sets of data in a matrix. I want to plot on set and then use a keyboard input to move to another one. It's simply possible this way:
for t=1:N
plot(data(:,t))
pause
end
but I want to move forward and backward in time t
(e.g. using arrows). OK, it could be done like this:
direction = input('Forward or backward?','s')
if direction=='forward'
plot(data(:,ii+1))
else
plot(data(:,ii-1))
end
but isn't there something more elegant? (On one click without getting the figure of the sight - it's a big full sreen figure.)
This demo shows you how to use either the left and right arrows of the keyboard to switch data set or even the mouse wheel.
It uses the
KeyPressFcn
and/orWindowScrollWheelFcn
event of the figure.This will roll over when the end of the data set is reached.
done a little gif too but it's a lot less impressive because it does not show the keyboard/mouse action, only the graph updates :
You can use mouse clicks combined with
ginput
. What you can do is put your code in awhile
loop and wait for the user to click somewhere on the screen.ginput
pauses until some user input has taken place. This must be done on the figure screen though. When you're done, check to see which key was pushed then act accordingly. Left click would mean that you would plot the next set of data while right click would mean that you plot the previous set of data.You'd call
ginput
this way:x
andy
denote thex
andy
coordinates of where an action occurred in the figure window andb
is the button you pushed. You actually don't need the spatial coordinates and so you can ignore them when you're calling the function.The value of 1 gets assigned a left click and the value of 3 gets assigned a right click. Also, escape (on my computer) gets assigned a value of 27. Therefore, you could have a
while
loop that keeps cycling and plotting things on mouse clicks until you push escape. When escape happens, quit the loop and stop asking for input.However, if you want to use arrow keys, on my computer, the value of 28 means left arrow and the value of 29 means right arrow. I'll put comments in the code below if you desire to use arrow keys.
Do something like this:
Here's an animated GIF demonstrating its use: