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

조회 수: 48 (최근 30일)
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
Stephen
Stephen 2016년 9월 24일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 9월 24일
편집: Walter Roberson 2016년 9월 24일
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
Stephen
Stephen 2016년 9월 25일
Again, many thanks! It was getting the name of a key that had me flummoxed.
Walter Roberson
Walter Roberson 2016년 9월 25일
CurrentKey is the trick there. But for future you should look at figure keypressfcn and windowkeypressfcn callback

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by