How can I use up and down arrow keys to scroll through my plots in MATLAB? (similar to a query on 21 Jan 2010)

I have a similar question as asked back on Jan 21, 2010. But being a rank beginner, I couldn't understand or implement that answer. I have a large dataset of biological samples that I plot out as a graph, one sample at a time. The data are in an array with columns = sample number, and rows = measurements taken over time. I wrote a simple script to plot each sample and move forward through the dataset one sample at a time merely by hitting any key. For instance, here is my script for 10 samples measured 600 times (10 columns, 600 rows):
for k = 1:10
trace = (data(:,k));
plot(trace)
hold on;
title(['cell # ',num2str(k),'']);
hold off;
waitforbuttonpress
end
Is there a simple way to alter this script so that I can scroll backward as well as forward?
Thanks!

1 Comment

I neglected to mention in the above that my biological samples are "cells", hence the title for each plot (line 5). Also, the fact that there are 600 measurements (rows) is irrelevant of course. I just put that there in the event you wanted to create your own array, "data", to test out the script

Sign in to comment.

 Accepted Answer

fig = gcf;
ax = axes('Parent', fig);
max_k = 10;
k = 1;
while k <= max_k
trace = (data(:,k));
plot(ax, trace)
hold(ax, 'on');
title(ax, ['cell # ',num2str(k),'']);
hold(ax, 'off');
was_a_key = waitforbuttonpress;
if was_a_key && strcmp(get(fig, 'CurrentKey'), 'uparrow')
k = k - 1;
else
k = k + 1;
end
end

4 Comments

Thanks for such a prompt reply, Walter! Being a newbie, I am slowly working my way through each line so I can understand the script and use it in a more complex one I am writing that does additional things (like adding vertical lines at specific time points). Would it be too much to ask you to explain in words these two lines of script?
was_a_key = waitforbuttonpress;
if was_a_key && strcmp(get(fig, 'CurrentKey'), 'uparrow')
k = k - 1;
I get that "waitforbuttonpress" stops the script and returns a 1 when I hit a down or up key. Can you describe the next line, though.
waitforbuttonpress returns 0 if the mouse was clicked, and returns 1 if some key was pressed. If the mouse was clicked (any mouse button), the code assumes that you want to proceed to the next plot as usual. But if a key was pressed then the first part of if was_a_key && would succeed, so the second part would be executed. The second part uses get(fig, 'CurrentKey') to get the name of the key was last pressed -- something that would not make sense to do if you had clicked the mouse instead of pressing a key. The key name is then compared to 'uparrow', which is the name generated internally when you press the up arrow key. If that exact key was pressed, then k is decremented so that you will go backwards in the sequence; in any other case k is increment so that you will go forward in the sequence.
The rest is handled by the while loop, which is instructed to keep going until eventually you get beyond the last plot, having moved forward and backwards as you wanted.
As I write this, I realize that where I wrote
k = k - 1;
you should instead have
k = max(1, k - 1);
This prevents you from scrolling back to before the first plot, which would otherwise create indexing problems.
Again, many thanks! It was getting the name of a key that had me flummoxed.
CurrentKey is the trick there. But for future you should look at figure keypressfcn and windowkeypressfcn callback

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!