Pan view of graph interactively
h = pan(figure_handle)
Access the following properties of pan mode objects via get and modify some of them using set:
Enable 'on'|'off' — Specifies whether this figure mode is currently enabled on the figure
Motion 'horizontal'|'vertical'|'both' — The type of panning enabled for the figure
FigureHandle <handle> — The associated figure handle, a read-only property that cannot be set
You can program the following callbacks for pan mode operations.
ButtonDownFilter <function_handle> — Function to intercept ButtonDown events
The application can inhibit the panning operation under circumstances the programmer defines, depending on what the callback returns. The input function handle should reference a function with two implicit arguments (similar to Handle Graphics® object callbacks):
function [res] = myfunction(obj,event_obj) % obj handle to the object clicked on % event_obj event data (empty in this release) % res [output] a logical flag to determine whether the pan % operation should take place(for 'res' set to 'false') % or the 'ButtonDownFcn' property of the object should % take precedence (when 'res' is 'true')
ActionPreCallback <function_handle> — Function to execute before panning
Set this callback to if you need to execute code when a pan operation begins. The function handle should reference a function with two implicit arguments (similar to Handle Graphics object callbacks):
function myfunction(obj,event_obj) % obj handle to the figure that has been clicked on % event_obj object containing struct of event data
The event data struct has the following field:
The handle of the axes that is being panned
ActionPostCallback <function_handle> — Function to execute after panning
Set this callback if you need to execute code when a pan operation ends. The function handle should reference a function with two implicit arguments (similar to Handle Graphics object callbacks):
function myfunction(obj,event_obj) % obj handle to the figure that has been clicked on % event_obj object containing struct of event data % (same as the event data of the % 'ActionPreCallback' callback)
The following functions in pan mode query and set certain of its properties.
flags = isAllowAxesPan(h,axes) — Function querying permission to pan axes
Calling the function isAllowAxesPan on the pan object, h, with a vector of axes handles, axes, as input returns a logical array of the same dimension as the axes handle vector, which indicates whether a pan operation is permitted on the axes objects.
setAllowAxesPan(h,axes,flag) — Function to set permission to pan axes
Calling the function setAllowAxesPan on the pan object, h, with a vector of axes handles, axes, and a logical scalar, flag, either allows or disallows a pan operation on the axes objects.
info = getAxesPanMotion(h,axes) — Function to get style of pan operations
Calling the function getAxesPanMotion on the pan object, h, with a vector of axes handles, axes, as input will return a character cell array of the same dimension as the axes handle vector, which indicates the type of pan operation for each axes. Possible values for the type of operation are 'horizontal', 'vertical' or 'both'.
setAxesPanMotion(h,axes,style) — Function to set style of pan operations
Calling the function setAxesPanMotion on the pan object, h, with a vector of axes handles, axes, and a character array, style, sets the style of panning on each axes.
Plot a graph and turn on Pan mode:
plot(magic(10)); pan on % pan on the plot
Constrain pan to x-axis using set:
plot(magic(10)); h = pan; set(h,'Motion','horizontal','Enable','on'); % pan on the plot in the horizontal direction.
Create four axes as subplots and give each one a different panning behavior:
ax1 = subplot(2,2,1); plot(1:10); h = pan; ax2 = subplot(2,2,2); plot(rand(3)); setAllowAxesPan(h,ax2,false); ax3 = subplot(2,2,3); plot(peaks); setAxesPanMotion(h,ax3,'horizontal'); ax4 = subplot(2,2,4); contour(peaks); setAxesPanMotion(h,ax4,'vertical'); % pan on the plots.
Create a buttonDown callback for pan mode objects to trigger. Copy the following code to a new file, execute it, and observe panning behavior:
function demo % Allow a line to have its own 'ButtonDownFcn' callback. hLine = plot(rand(1,10)); set(hLine,'ButtonDownFcn','disp(''This executes'')'); set(hLine,'Tag','DoNotIgnore'); h = pan; set(h,'ButtonDownFilter',@mycallback); set(h,'Enable','on'); % mouse click on the line % function [flag] = mycallback(obj,event_obj) % If the tag of the object is 'DoNotIgnore', then % return true. % Indicate what the target is. disp(['Clicked ' get(obj,'Type') ' object']) objTag = get(obj,'Tag'); if strcmpi(objTag,'DoNotIgnore') flag = true; else flag = false; end
Create callbacks for pre- and post-ButtonDown events for pan mode objects to trigger. Copy the following code to a new file, execute it, and observe panning behavior:
function demo % Listen to pan events plot(1:10); h = pan; set(h,'ActionPreCallback',@myprecallback); set(h,'ActionPostCallback',@mypostcallback); set(h,'Enable','on'); % function myprecallback(obj,evd) disp('A pan is about to occur.'); % function mypostcallback(obj,evd) newLim = get(evd.Axes,'XLim'); msgbox(sprintf('The new X-Limits are [%.2f,%.2f].',newLim));
Coding a context menu that lets the user to switch to Zoom mode by right-clicking:
figure; plot(magic(10)); hCM = uicontextmenu; hMenu = uimenu('Parent',hCM,'Label','Switch to zoom',... 'Callback','zoom(gcbf,''on'')'); hPan = pan(gcf); set(hPan,'UIContextMenu',hCM); pan('on')
You cannot add items to the built-in pan context menu, but you can replace it with your own.
Use the Pan tool on the figure toolbar to enable and disable pan mode on a plot, or select Pan from the figure's Tools menu. For details, see Panning — Shifting Your View of the Graph.
You can create a pan mode object once and use it to customize the behavior of different axes, as Example 3 illustrates. You can also change its callback functions on the fly.
Note: Do not change figure callbacks within an interactive mode. While a mode is active (when panning, zooming, etc.), you will receive a warning if you attempt to change any of the figure's callbacks and the operation will not succeed. The one exception to this rule is the figure WindowButtonMotionFcn callback, which can be changed from within a mode. Therefore, if you are creating a GUI that updates a figure's callbacks, the GUI should some keep track of which interactive mode is active, if any, before attempting to do this.
When you assign different pan behaviors to different subplot axes via a mode object and then link them using the linkaxes function, the behavior of the axes you manipulate with the mouse carries over to the linked axes, regardless of the behavior you previously set for the other axes.