Hi all,
I want to assign the key32 (spacebar) to ginput. I have a figure with spikes and I want to select manually each spike. Once I selected to spike, the code continues selecting other points using ginput.
The problem is that I took this code for zoom and span the figure by pressing keys. My aim is to be able to zoom and span the figure at any time and continue selecting points with the spacebar. Sound easy but I cannot include the code for zoom and span in the main code for ginput.
Here the zoom and span code made by Ned Gulley: https://es.mathworks.com/matlabcentral/fileexchange/3090-zoom-keys
function axdrag(action)
persistent x0 dx
if nargin < 1
action = 'initialize';
end
zoomFactor = 0.9;
panFactor = 0.02;
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;
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
elseif currChar=='n'
axis normal
elseif currChar=='g'
grid
elseif currChar==28
xLim=get(gca,'XLim');
xLimNew = xLim + panFactor*diff(xLim);
set(gca,'XLim',xLimNew)
elseif currChar==29
xLim=get(gca,'XLim');
xLimNew = xLim - panFactor*diff(xLim);
set(gca,'XLim',xLimNew)
elseif currChar==30
yLim=get(gca,'YLim');
yLimNew = yLim - panFactor*diff(yLim);
set(gca,'YLim',yLimNew)
elseif currChar==31
yLim=get(gca,'YLim');
yLimNew = yLim + panFactor*diff(yLim);
set(gca,'YLim',yLimNew)
elseif abs(currChar)==32
if isempty(get(gca,'XTick'))
set(gca,'XTickMode','auto','YTickMode','auto')
else
set(gca,'XTick',[],'YTick',[],'Box','on')
end
elseif (currChar=='x') || (currChar=='X')
if currChar == 'X'
zoomFactor=1/zoomFactor;
end
xLim=get(gca,'XLim');
xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2;
set(gca,'XLim',xLimNew)
elseif (currChar=='y') || (currChar=='Y')
if currChar == 'Y'
zoomFactor=1/zoomFactor;
end
yLim=get(gca,'YLim');
yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2;
set(gca,'YLim',yLimNew)
elseif (currChar=='z') || (currChar=='Z')
if currChar == 'Z'
zoomFactor=1/zoomFactor;
end
xLim=get(gca,'XLim');
yLim=get(gca,'YLim');
xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2;
yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2;
set(gca,'XLim',xLimNew,'YLim',yLimNew)
elseif currChar=='h'
if helpWasOff
str = { ...
' '
' AXDRAG. Keys you can use are:'
' '
' z, Z: zoom in, zoom out, both dimensions '
' x, X: zoom in, zoom out, x dimension only '
' y, Y: zoom in, zoom out, y dimension only '
' arrow keys: pan the data'
' a: axis auto'
' n: axis normal'
' e: axis equal'
' g: toggle grid state'
' spacebar: toggle axis tick display state'
' h: help'
' '
' Press ''h'' again to dismiss this message'
' ' ...
};
helpTextAxis = axes( ...
'Tag','axdraghelpaxis', ...
'Units','characters', ...
'Position',[2 3 76 16], ...
'Visible','off');
text(0,1,str, ...
'Parent',helpTextAxis, ...
'VerticalAlignment','top', ...
'BackgroundColor',[1 1 0.8], ...
'FontName','courier', ...
'FontSize',10);
end
end
end
end
This is the main code, where I choose the points in the figure:
clear all; close all;
clc;
fileName = 'First third_b2310.mat';
load(fileName);
disp(fileName);
x = input('Time: ');
y = input ('Amplitude: ');
cont = 1;
mNum = 0;
fs=0;
fl=0;
KnR=0;
null=0;
mainDir = [cd '\first 6 spikes/'];
mkdir(mainDir);
addpath(genpath('C:\Users\physiol\Desktop\Matlab scripts for Amp\Data\Spikes not analyzed'));
while cont==1
f1=figure(1);
set(f1,'units','normalized','position',[0 0 1 1],'NumberTitle','on','ToolBar', 'None');
f1.WindowState = 'maximized';
hold on;box on; grid on;
plot(x,y,'b');
ylabel('Amplitud (pA)');
xlabel({'Time (s)'; ['foot-Spike:' num2str(fs) ' Flicker:' num2str(fl)...
' Kiss and Run:' num2str(KnR) ' Null:' num2str(null)]})
axis([x(1) x(length(x)) min(abs(y)) max(y)]);
title('Select two points around a ROI', 'interpreter','none');
ylim([-100 200]);
yline(7,'-.k');
pause();
[px_1, py_1, ~] = ginput (1);
line([px_1 px_1], [min(y) max(y)], 'linestyle', '-', 'color', 'r');
[~, h1] = min(abs(px_1*ones(length(x),1)-x));
line(xlim, [y(h1) y(h1)], 'linestyle', '-', 'color', 'g');
plot(x(h1),y(h1),'ok');
pause();
[px_2, py_2, buttons] = ginput (1);
line([px_2 px_2], [min(y) max(y)], 'linestyle', '-', 'color', 'r');
[aux, h2] = min(abs(px_2*ones(length(x),1)-x));
line(xlim, [y(h2) y(h2)], 'linestyle', '-', 'color', 'g');
plot(x(h2),y(h2),'ok');
hold on;
T = x(h1:h2);
I = y(h1:h2);
list = {'Foot Signal of the current spike','Another event (Flicker)',...
'Spike (Kiss&Run)','Null event','Redo de selecction','Close all'};
result = listdlg('PromptString',{'So far ' num2str(mNum) ' regions were measured'}, 'SelectionMode','single','ListString',list);
if result==1
mNum = mNum+1;
fs= fs+1;
save([mainDir 'Spike No' num2str(mNum,'%d') '.mat'], 'T','I')
pause ();
[px_0, py_0, ~] = ginput (1);
line([px_0 px_0], [min(y) max(y)], 'linestyle', ':', 'color', 'r');
[~, h0] = min(abs(px_0*ones(length(x),1)-x));
line(xlim, [y(h0) y(h0)], 'linestyle', ':', 'color', 'r');
plot(x(h0),y(h0),'ok');
hold on;
pause();
[px_1, py_1, ~] = ginput (1);
line([px_1 px_1], [min(y) max(y)], 'linestyle', '-', 'color', 'y');
[~, h1] = min(abs(px_1*ones(length(x),1)-x));
line(xlim, [y(h1) y(h1)], 'linestyle', '-', 'color', 'g');
plot(x(h1),y(h1),'ok');
hold on;
pause();
[px_2, py_2, buttons] = ginput (1);
line([px_2 px_2], [min(y) max(y)], 'linestyle', '-', 'color', 'y');
[aux, h2] = min(abs(px_2*ones(length(x),1)-x));
line(xlim, [y(h2) y(h2)], 'linestyle', '-', 'color', 'g');
plot(x(h2),y(h2),'ok');
hold on;
Tbn = x(h0:h1);
Ibn = y(h0:h1);
Tf = x(h1:h2);
If = y(h1:h2);
save([mainDir 'Foot signal No' num2str(mNum,'%d') '.mat'], 'Tf','If','Tbn','Ibn');
elseif result==2
mNum = mNum+1;
fl=fl+1;
save([mainDir 'Flicker No%d' num2str(mNum,'%d') '.mat'], 'T','I');
elseif result==3
mNum = mNum+1;
KnR=KnR+1;
save([mainDir 'Spike Kiss&Run No%d' num2str(mNum,'%d') '.mat'], 'T','I');
elseif result==4
mNum = mNum+1;
null=null+1;
save([mainDir 'Null event No%d' num2str(mNum,'%d') '.mat'], 'T','I');
elseif result==5
continue
else result==6
cont=0
return
end
end
I appreciate your collaboration.