grabDataFromCursor.m 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
function Data = grabDataFromCursor(nbData)
% Collect movement data from mouse cursor
% Sylvain Calinon, 2015

nbSamples = 0;
exitFlag = 0;
runningFlag = 0;
if nargin<1
	nbData = 100;
end

fig = figure('position',[10,10,700,700]); hold on;
set(gca,'Xtick',[]); set(gca,'Ytick',[]);
setappdata(gcf,'motion',[]);
setappdata(gcf,'data',[]);
setappdata(gcf,'nbSamples',nbSamples);
setappdata(gcf,'exitFlag',exitFlag);
setappdata(gcf,'runningFlag',runningFlag);
set(fig,'WindowButtonDownFcn',{@wbd});
axis([-10 10 -10 10]);
plot(0,0,'k+');

motion=[];
Data=[];
while exitFlag==0
	drawnow;
	if runningFlag==1 
		cur_point = get(gca,'Currentpoint');
		motion = [motion cur_point(1,1:2)'];
		plot(motion(1,end),motion(2,end),'k.','markerSize',1);
		runningFlag = getappdata(gcf,'runningFlag');
		if(runningFlag==0)
			duration = getappdata(gcf,'duration');
			%Resampling
			nbDataTmp = size(motion,2);
			xx = linspace(1,nbDataTmp,nbData);
			motion = spline(1:nbDataTmp, motion, xx);
			%motion = interp1(1:nbDataTmp, motion', xx)';
			motion_smooth = motion;
			for n=1:1
				motion_smooth(1,:) = smooth(motion_smooth(1,:),1);
				motion_smooth(2,:) = smooth(motion_smooth(2,:),1);
			end
			plot(motion_smooth(1,:),motion_smooth(2,:), 'r', 'lineWidth', 1);
			nbSamples = nbSamples + 1;
			%Data = [Data, [1:nbData; motion_smooth]]; %With time entries
			Data = [Data, motion_smooth]; %Without time entries
			motion = [];
		end
	end
	runningFlag = getappdata(gcf,'runningFlag');
	exitFlag = getappdata(gcf,'exitFlag');
end

close all


% -----------------------------------------------------------------------
function wbd(h,evd) % executes when the mouse button is pressed
muoseside = get(gcf,'SelectionType');
if strcmp(muoseside,'alt')==1
	setappdata(gcf,'exitFlag',1);
	return;
end
%get the values and store them in the figure's appdata
props.WindowButtonMotionFcn = get(h,'WindowButtonMotionFcn');
props.WindowButtonUpFcn = get(h,'WindowButtonUpFcn');
setappdata(h,'TestGuiCallbacks',props);
set(h,'WindowButtonMotionFcn',{@wbm});
set(h,'WindowButtonUpFcn',{@wbu});
setappdata(gcf,'runningFlag',1);
tic
% -----------------------------------------------------------------------
function wbm(h,evd) % executes while the mouse moves
% -----------------------------------------------------------------------
function wbu(h,evd) % executes when the mouse button is released
setappdata(gcf,'runningFlag',0);
duration = toc;
setappdata(gcf,'duration',duration);
%get the properties and restore them
props = getappdata(h,'TestGuiCallbacks');
set(h,props);