Problems including a new KeyPressFcn to call a function in axdrag file

2 次查看(过去 30 天)
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 个评论
Jose Rego Terol
Jose Rego Terol 2020-5-5
So, I solve the problem: 4 different scripts, and global variables. Without global I couldn't pass out the variables. I tried to bridge axdrag, but imposible.
function plot_v1
global u v
S=load('F310120.mat');
u=S.T;
v=S.I;
plot(u,v,'b');
axdrag;
end
axdrag
function axdrag(action)
% Copyright 2018, The MathWorks, Inc.
% Ned Gulley
% global px_1 px_2
persistent x0 dx
if nargin < 1
action = 'initialize';
end
% Use these variables to change the zoom and pan amounts
zoomFactor = 0.9;
panFactor = 0.02;
% Get rid of the help window if it's being displayed
helpTextAxis = findobj(gcbf,'Type','axes','Tag','axdraghelpaxis');
if isempty(helpTextAxis)
helpWasOff = 1;
else
helpWasOff = 0;
delete(helpTextAxis);
end
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;
otherthing 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
elseif currChar=='g' || (currChar=='G')
if currChar == 'g'
first_ginput;
else
second_ginput
end
Last 2 scripts:
function first_ginput
global u h1
[px_1,~] = myginput(1,'crosshair');
h1=nearestpoint(px_1,u);
end
function second_ginput
global u v h1 h2
[px_2,~] = myginput(1,'crosshair');
h2=nearestpoint(px_2,u);
T=u(h1:h2);
I=v(h1:h2);
[~,b]=max(I);%Finds the row (b) when I is max (a)
Time=T(b);%Gives you the second when I is max
Amplitude=round((max(I)),8);%Max I (rounded) of the spike
halfMax = max(I)/2;
t=table(Time,Amplitude,halfMax);
writetable(t,'tableforme.xls','Sheet',1);
end
I tried to pass out the variable u and v using input arguments from the first script to the latest two scripts but I couldn´t do it. Global could be bad, but in my case is the best for this situation.
Thanks Tim for warning me that KeyPressFcn does not pass out variables. I did not know it.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by