How to use uipushtool to click and drag plots in figure?

Hi, I am fairly new with the uipushtool in MATLAB. I want to click and drag plots in a figure by pressing a button I have created. The Click-n'-Drag m-file Jean-Pascal Rueff created : https://www.mathworks.com/matlabcentral/fileexchange/5751-click-n--drag-plot allows the user to automatically click and drag the plots in the figure created without pressing any previous buttons in the toolbar. I have tried to create a button to complete such task instead, but I keep getting an error message stating:
Error in interactive_move>dragger (line 80)
switch type
Error while evaluating PushTool ClickedCallback.
-----------------------------------------------------------------------------
Line 80 begins with switch type
(Please help)

2 Comments

Miles - please clarify what you mean by I have tried to create a button to complete... When I run the attached code (which I'm assuming you've integrated Jean-Pascal's code into), then I can easily move the plots around. Are you saying that you get an error when you run the attached code?
I'm using MATLAB r2014a. Which version are you using?
I want to click and drag plots ONLY when I select the drag button I have created in the toolbar on the figure. When the drag button is clicked, that's when I get the error message.

Sign in to comment.

 Accepted Answer

Miles - ok, when I push the button, I see a slightly different error
Error using interactive_move>dragger (line 80)
Not enough input arguments.
Look closely at the dragger function signature
function dragger(~,~,type)
It is expecting three input parameters. The first two are typically the source handle and some event data. But the third, in this case, appears to be a custom parameter which you need to define when you add dragger as the callback to the uipushtool
p = uipushtool(h,'CData',b,'TooltipString','Drag Plot',...
'Separator','on','ClickedCallback',@dragger);
Note how the other callbacks do this
set(gcf, 'windowbuttondownfcn', {@dragger,1});
set(gcf, 'windowbuttonmotionfcn', {@dragger,2});
set(gcf, 'windowbuttonupfcn', {@dragger,3});
set(gcf, 'keypressfcn', {@dragger,4});
So you need to pass in a "type" parameter.
But I don't know if this will fix your problem which is that you only want to drag plots when the button is clicked. I think that the callback for this tool should be something more like
function enableDragger(~,~)
if isempty(get(gcf, 'windowbuttondownfcn'))
set(gcf, 'windowbuttondownfcn', {@dragger,1});
set(gcf, 'windowbuttonmotionfcn', {@dragger,2});
set(gcf, 'windowbuttonupfcn', {@dragger,3});
set(gcf, 'keypressfcn', {@dragger,4});
else
set(gcf, 'windowbuttondownfcn', []);
set(gcf, 'windowbuttonmotionfcn', []);
set(gcf, 'windowbuttonupfcn', []);
set(gcf, 'keypressfcn', []);
end
So you add (or remove) the dragger function to (or from) the above callbacks. The only time that you can drag is when you've pressed the button (I'm assuming that the only code you have to add dragger as a callback to the four functions is through the enableDragger function) and that when you press the button again, you won't be able to drag.

2 Comments

Miles' answer moved here
Geoff -- The code runs perfectly now! I removed: ---------------------------------------------
set(gcf, 'windowbuttondownfcn', {@dragger,1}); set(gcf, 'windowbuttonmotionfcn', {@dragger,2});
set(gcf, 'windowbuttonupfcn', {@dragger,3}); set(gcf, 'keypressfcn', {@dragger,4});
-------------------------------------------------------------------------------------------
and then added in your enableDragger function. I also edited my uipushtool variable as such:
------------------------------
p = uipushtool(h,'CData',b,'TooltipString','Drag Plot',...
'Separator','on','ClickedCallback',@enableDragger);
--------------------
Thank you very much for your time & efforts
Glad that I was able to help, Miles!

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!