我正在编写一个脚本,该脚本将创建2个支持,并具有一个滚动两个图的x轴的滑块,或2个分别控制每个子图x轴的滑块。
我一直在为滑子使用改编版的史蒂文洛兹FileExchange scrolling plot demo。
现在,它将仅更新最新的绘图(因为其回调函数中当前正在使用gca
)。我尝试用所需的轴(变量gca
或first_plot
)替换second_plot
,但这似乎不起作用。
然后我的问题是,我该如何调整此功能以同时控制两个图或每个图?这是我正在编写的脚本的示例:
x=0:1e-2:2*pi;
y=sin(x);
dx=2;
first_plot = subplot(2,1,1);
plot(x, y);
scrollplot(dx, x)
%Plot the respiration and probe data with scrolling bar
second_plot = subplot(2,1,2);
plot(x, y);
scrollplot(dx,x)
% dx is the width of the axis 'window'
function scrollplot(dx, x)
a=gca;
% Set appropriate axis limits and settings
set(gcf,'doublebuffer','on');
set(a,'xlim',[0 dx]);
% 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 dx]));
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);
end
谢谢!
答案 0 :(得分:1)
您快到了,但是从仅从一组轴修改代码时就会遇到一些结构性问题。
关键是将回调函数从字符串更改为实际的本地函数。这使得处理回调变得更加简单!
我已将您的代码修改为可用于两个(或更多)轴。请注意,我们只需要设置一次滚动条!您正在为每个轴进行设置(滚动条相互堆叠),并且两个滚动条仅在gca
上操作。仅命名轴是不足以更改gca
的,您必须使用这些变量!我已将轴分配给数组以便于操作。
请查看评论以获取详细信息:
x=0:1e-2:2*pi;
y=sin(x);
% dx is the width of the axis 'window'
dx=2;
% Initialise the figure once, and we only need to set the properties once
fig = figure(1); clf;
set( fig, 'doublebuffer', 'on');
% Create a placeholder for axes objects
ax = gobjects( 2, 1 );
% Create plots, storing them in the axes object
ax(1) = subplot(2,1,1);
plot(x, y);
ax(2) = subplot(2,1,2);
plot(x, y);
% Set up the scroller for the array of axes objects in 'ax'
scrollplot( dx, x, ax)
function scrollplot( dx, x, ax )
% Set appropriate axis limits
for ii = 1:numel(ax)
set( ax(ii), 'xlim', [0 dx] );
end
% Create Uicontrol slider
% The callback is another local function, this gives us more
% flexibility than a character array.
uicontrol('style','slider',...
'units', 'normalized', 'position', [0.1 0.01 0.8 0.05],...
'callback', @(slider, ~) scrollcallback( ax, dx, slider ), ...
'min', 0, 'max', max(x)-dx );
end
function scrollcallback( ax, dx, slider, varargin )
% Scroller callback loops through the axes objects and updates the xlim
val = slider.Value;
for ii = 1:numel(ax)
set( ax(ii), 'xlim', val + [0, dx] );
end
end