image thumbnail
from keyControlPlugin by georg
A figure plugin class usable as keyPress callback Function.

testkeyControlPlugin.m
%script TESTPLOTCONTROL
%   TESTPLOTCONTROL is a short script that tests and explains the
%   functionality of the keyControlPlugin class.
%   
%   georg ogris ::: spantec.at ::: fall 2011

%% generate some data
t = (0:0.01:100).' ;
T = length(t) ;
y = [sin(t.^2)-cos(t).^2  cos(t).^2.*sin(t.^2)] ;
r = randn(T,2)/10 ;
y = y+r ;

%% basic example
c = keyControlPlugin('scrollFactor',pi/4) ;
plot(t,sin(t)) ;
set(gcf,'KeyPressFcn',@c.keyPressFunction)

%% shortcomings
warning(['In case the figure is not in default mode, the plugin may fail;', ...
    'it may even fail to be initialized.', ...
    'You will either notice no change or see a warning message.'])

%% subplot example
% in case the figure contains several subplots the cummulated range will 
% be used for the maximum ratings
kcp1 = keyControlPlugin() ;
figure('KeyPressFcn',@kcp1.keyPressFunction)
subplot(2,1,1)
plot(t-10,y(:,1))
grid minor
subplot(2,1,2)
plot(t+10,y(:,2))
grid minor

%% for the next example it is better to disable the y-zoom
kcp2 = keyControlPlugin('ydisabled',true) ;
figure('KeyPressFcn',@kcp2.keyPressFunction)
subplot(2,1,1)
plot(t,y(:,1)-100)
grid minor
subplot(2,1,2)
plot(t,y(:,2)+100)
grid minor

%% you can also set how fast you want to scroll and zoom ...
kcp3 = keyControlPlugin('scrollFactor',1,'zoomFactor',0.75) ;
figure('KeyPressFcn',@kcp3.keyPressFunction)
subplot(2,1,1)
plot(t-10,y(:,1))
grid minor
subplot(2,1,2)
plot(t+10,y(:,2))
grid minor

%% ... or change it at some later time
%kcp3.scrollFactor = 0.1 ;
%kcp3.zoomFactor = 0.5 ;

%% use it in combination with linkaxes in order to control multiple figures
% be aware that the figure using the plugin, also controls the zoom and scroll
% range (which is in fact a problem in the example below, i.e. you are not
% able to see the end of the data of the second figure) 
kcp4 = keyControlPlugin() ;
figure('KeyPressFcn',@kcp4.keyPressFunction)
plot(t-10,y(:,1))
grid minor
ax = gca ;
figure
plot(t+10,y(:,2))
grid minor
ax = [ax gca] ;
linkaxes(ax,'xy')

Contact us