How do I use a slider to scroll through an axis or a color bar?
15 views (last 30 days)
Show older comments
How do I use a slider to scroll through an axis or a color bar?
I have a long set of data and I want to display a portion of it on an axis at a time. I also want to scroll through the colors in the colorbar so I can focus more precisely on a certain range of colors. Can I do this in MATLAB?
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
Here is a small example which shows how to scroll through an axis using a silder:
function scrollplotdemo
%%%%%Generate and plot data
x=0:1e-2:2*pi;
y=sin(x);
dx=2;
% dx is the width of the axis 'window'
a=gca;
p=plot(x,y);
%%%%%Set appropriate axis limits and settings
set(gcf,'doublebuffer','on');
% This avoids flickering when updating the axis
set(a,'xlim',[0 dx]);
set(a,'ylim',[min(y) max(y)]);
%%%%%Generate constants for use in uicontrol initialization
pos=get(a,'position');
Newpos=[pos(1) pos(2)-0.1 pos(3) 0.05];
% This will create a slider which is just underneath the axis
% but still leaves room for the axis labels above the slider
xmax=max(x);
S=['set(gca,''xlim'',get(gcbo,''value'')+[0 ' num2str(dx) '])'];
% Setting up callback string to modify XLim of axis (gca)
% based on the position of the slider (gcbo)
%%%%%Creating Uicontrol
h=uicontrol('style','slider',...
'units','normalized','position',Newpos,...
'callback',S,'min',0,'max',xmax-dx);
Below is an example which shows how to use a slider uicontrol to scroll through the values of a COLORBAR.
function slide1
h = surf(peaks(20));
CD = get(h,'cdata');
Min = min(CD(:));
Max = max(CD(:));
cb=colorbar;
DeltaC = (Max-Min)/10;
set(cb,'units','normalized','ylim',Min+[0 1]);
Pos=get(cb,'position');
S=['set(findobj(gcf,''tag'',''Colorbar''),''YLim'',get(gcbo,''Value'')+[0 ' num2str(DeltaC) '])'];
uic=uicontrol('style','slider','units','normalized',...
'position',[Pos(1)+0.11 Pos(2) 0.04 Pos(4)],...
'min',Min,'max',Max-DeltaC,'value',Min,...
'callback',S);
0 Comments
More Answers (0)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!