Problems including a new KeyPressFcn to call a function in axdrag file
Show older comments
Hi all,
I am using this file (axdrag) with my plot. I included new lines to call a function when the key 'g' is pressed but it does not work. There is not error, so the coding is correct, but somewhat these new lines are not doing the task in the plot.
Here is part of the code of axdrag
function axdrag(action)
if nargin < 1
action = 'initialize';
end
% Use these variables to change the zoom and pan amounts
zoomFactor = 0.9;
panFactor = 0.02;
%Code continues
switch action
case 'initialize'
set(gca,'ButtonDownFcn','axdrag start')
set(gcf,'KeyPressFcn','axdrag keypress')
set(gcf,'DoubleBuffer','on')
case 'start'
set(gcbf,'Units','pixel');
set(gca,'Units','pixel');
set(gcbf,'WindowButtonMotionFcn','axdrag move')
set(gcbf,'WindowButtonUpFcn','axdrag stop')
currentPoint = get(gcbf,'CurrentPoint');
x0 = currentPoint;
axdrag move
case 'move'
currentPoint = get(gcbf,'CurrentPoint');
dx = currentPoint - x0;
x0 = currentPoint;
ap = get(gca,'Position');
xLim = get(gca,'XLim');
yLim = get(gca,'YLim');
set(gca,'XLim',xLim-(diff(xLim)*dx(1)/ap(3)), ...
'YLim',yLim-(diff(yLim)*dx(2)/ap(4)));
case 'stop'
set(gcbf,'WindowButtonMotionFcn','')
set(gcbf,'WindowButtonUpFcn','')
set(gcbf,'Units','normalized');
set(gca,'Units','normalized');
case 'keypress'
currChar = get(gcbf,'CurrentCharacter');
if isempty(currChar)
return
end
if currChar=='a'
axis auto
elseif currChar=='e'
axis equal
% Code continues
end
end
end
Then, I included this line, but it is doing nothing
elseif currChar=='g'
myginput(1)
I expected to enable the selection of one point in my plot. But as I said, nothing is running.
Also, I tried with this (simpler task), but again, it does not work.
elseif currChar=='g'
zoom on
The way I call the function in the Command Window:
plot(x,y)
axdrag
How can I call a function that works in the plot by pressing a key?
Thanks,
Jose
7 Comments
Tommy
on 2 May 2020
elseif currChar=='g'
grid
Are these lines still present in your updated file? If so, and your code comes after, then pressing g will only call grid.
Jose Rego Terol
on 2 May 2020
Tommy
on 2 May 2020
No worries, glad it is working!
Jose Rego Terol
on 4 May 2020
When you first call your function:
[px_1]=axdrag2;
you are calling it with no arguments. So, inside your function, this if statement evaluates to true:
if nargin < 1
action = 'initialize';
end
and the following code runs in the switch case statement:
set(gca,'ButtonDownFcn','axdrag start')
set(gcf,'KeyPressFcn','axdrag keypress')
set(gcf,'DoubleBuffer','on')
Then the function exits, and as px_1 is never defined, you get an error.
This function, axdrag, is designed so that when you next click on the axes or press a key, the function is called again, this time with a specific argument. When you press the 'g' key, the figure's KeyPressFcn is called. The KeyPressFcn is 'axdrag keypress', a.k.a. axdrag('keypress'). There are two problems:
- Will your program know what axdrag is? It won't if axdrag is not on the path somewhere, and if it is on the path, well, now the original axdrag is being used, not your updated axdrag2. You would have to change all of those 'axdrag arg1' strings to 'axdrag2 arg1'.
- The idea of passing a variable out of the KeyPressFcn does not really make sense.
Note that the ability to zoom and pan are enabled by default now. If these are the only reasons you are using axdrag, I wouldn't use it.
How is your function, plot_v1(), supposed to wait until the user has pressed 'g' and then clicked in the axes before it analyzes the data? You'd either have to play around with blocking program execution or (really, the proper way to do this) set up your own KeyPressFcn to call myginput() in response to 'g' key presses and then perform the data analysis within the KeyPressFcn, or in some other function which gets called by the KeyPressFcn. In this case, plot_v1() will (probably) finish executing long before the user ever presses 'g'. Besides, the idea is to let the user press 'g' whenever they want - otherwise, you would just call ginput() right off the bat.
(edit) I would also post this and/or subsequent questions in a new post. If you comment a link here, I am more than happy to take a look!
Jose Rego Terol
on 5 May 2020
Tommy
on 5 May 2020
Fantastic, happy to help!
Answers (0)
Categories
Find more on Data Exploration 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!