Assign the space bar to ginput.

3 views (last 30 days)
Jose Rego Terol
Jose Rego Terol on 5 Feb 2020
Commented: Jose Rego Terol on 6 Feb 2020
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)
%AXDRAG Pan and zoom with simple keystrokes
% Use this tool to move quickly around the data displayed in a 2-D plot.
% Make sure the figure has focus, and then press any of the following
% keys to zoom in or out. Clicking and dragging will pan the data.
%
% Keys you can use are:
% z, Z: zoom in, zoom out, in 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
%
% Example
% c = pi*(1+sqrt(5))/2;
% x = 0:1000;
% r = 2.72378;
% z = cumsum(exp(i*(c*x.*x + r)));
% plot(real(z),imag(z));
% axdrag
% % Now click, drag, and use special keys ...
% Copyright 2018, The MathWorks, Inc.
% Ned Gulley
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;
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:
%% Before F5, change the name of the file!!
clear all; close all; %format compact;
clc;
fileName = 'First third_b2310.mat'; %Change to whatever file you want to detect the spikes
load(fileName);
disp(fileName);
% S = load(fileName);
% C = struct2cell(S);
x = input('Time: ');
y = input ('Amplitude: ');
cont = 1;
mNum = 0;
fs=0;
fl=0;
KnR=0;
null=0;
mainDir = [cd '\first 6 spikes/']; %Change the name of the folder where the spikes files will be.
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');%axis tight;
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)]); % Update plot
title('Select two points around a ROI', 'interpreter','none');
ylim([-100 200]);
yline(7,'-.k');
pause();
[px_1, py_1, ~] = ginput (1);
% Plot the vertical line at the x
line([px_1 px_1], [min(y) max(y)], 'linestyle', '-', 'color', 'r');
% Show selected point on curve
[~, 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');
% Show selected point on curve
[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');
%save([mainDir 'Spike No' num2str(mNum,'%d') '.mat'], 'x(h1)','y(h1)')
hold on;
%% Selected data within range:
T = x(h1:h2);
I = y(h1:h2);
% f2=figure(2);
% set(f2,'units','normalized','position',[0.05,0.05,0.9,0.9],'NumberTitle','off','ToolBar', 'None');
% hold on; box on; grid on;
% %plot(X,Y); axis tight;
% title(['Measured of the ROI #' num2str(mNum)]);
% %print('-dpdf',[mainDir 'ROI_No' num2str(mNum,'%d')]);
% %save([mainDir 'ROI_No' num2str(mNum,'%d') '.mat'], 'X','Y')
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);
% Plot the vertical line at the x
line([px_0 px_0], [min(y) max(y)], 'linestyle', ':', 'color', 'r');
% Show selected point on curve
[~, 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);
% Plot the vertical line at the x
line([px_1 px_1], [min(y) max(y)], 'linestyle', '-', 'color', 'y');
% Show selected point on curve
[~, 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);
%zoom out; % go to the original size of your image
line([px_2 px_2], [min(y) max(y)], 'linestyle', '-', 'color', 'y');
% Show selected point on curve
[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');
%save([mainDir 'Spike No' num2str(mNum,'%d') '.mat'], 'x(h1)','y(h1)')
hold on;
%% Selected data within range:
Tbn = x(h0:h1);
Ibn = y(h0:h1);
Tf = x(h1:h2);
If = y(h1:h2);
% f2=figure(2);
% set(f2,'units','normalized','position',[0.05,0.05,0.9,0.9],'NumberTitle','off','ToolBar', 'None');
% hold on; box on; grid on;
% % plot(X,Y); axis tight;
% title(['Measured of the foot signal spike #' num2str(mNum)]);
% %print('-dpdf',[mainDir 'ROI_No' num2str(mNum,'%d')]);
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.
  1 Comment
Jose Rego Terol
Jose Rego Terol on 6 Feb 2020
Alternatively, I call the script axdrag after the plot (line 47).
But I do not know how I can come back to the main script immediately after line 47. Because when I call the main script, the code starts again to read the script from the beggining.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!