Change plot interaction mode on the fly

조회 수: 2 (최근 30일)
Graham Rowe
Graham Rowe 2020년 12월 18일
댓글: Prudhvi Peddagoni 2021년 1월 4일
I'm trying to change the behavior of the mouse pointer in a 3d axes. When you first open a figure it seems to do something similar to what I want, but not including pan.
What I would like to do:
  • Left click and drag will pan
  • middle click and drag will rotate
  • scroll wheel will zoom in and out
  • double click: enables datacursormode (this is a nice to have, but less important that the other stuff)
This is what I've tried to get this to work so far:
fig = figure();
set(fig, 'WindowButtonDownFcn', @interactModeCallback); % should handle the button presses
set(fig, 'WindowScrollWheelFcn', @interactModeCallback); % should handle scroll wheel
ground = 10000*membrane(1,40)-10000; % example plot
function interactModeCallback(src, event)
click_type = get(src,'selectiontype');
even_type = event.EventName;
if strcmp('WindowMousePress', even_type) % Deal with all the click events
if strcmp('normal', click_type) % Left click will pan
pan on;
elseif strcmp('extend', click_type) % middle click will rotate
rotate3d on;
elseif strcmp('open', click_type) % double click will select points
datacursormode on;
elseif strcmp('WindowScrollWheel', even_type) % Scrolling will zoom
zoom on;
There are 2 issues I'm running into:
  1. It takes two button clicks to actually move the plot. i.e. you must click > release > click > drag. I want click > drag
  2. My callback function is only called once. Once I have clicked I am locked into that mode. I have tried to fix this by adding a corresponding callback function to turn off the interaction mode on WindowButtonUpFnc but it doesn't change anything.
Any help is appreciated.

채택된 답변

Prudhvi Peddagoni
Prudhvi Peddagoni 2020년 12월 29일
  1. when you click on the mouse, you are selecting pan mode. So you have to click again for the callback function in the pan to execute.
  2. Callbacks are disabled when settings like pan, zoom etc are enabled because they interfere with the callback functions of these modes. That is the reason you couldn't call the callback function more than once.
Hope this helps.
  댓글 수: 2
Graham Rowe
Graham Rowe 2021년 1월 2일
It does explain why I can't make it do what I want. There must be some work around though. Kind of annoying that the plotting capabilities in Matlab are so restrictive.
Thanks for your response.
Prudhvi Peddagoni
Prudhvi Peddagoni 2021년 1월 4일
You can find the workaround for this here.
Or you can just use the buttons showed in the image.

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

추가 답변 (0개)


Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by