function medit2(varargin);

switch nargin
    case 0
        fcn = 'setup';
    case 1
        fcn = varargin{1} ;
end

%===========================================================
switch fcn
    case 'setup'
        data.myname = mfilename;
        motionfig = figure(3);
        clf
        set(gcf,'doublebuffer','on')

        %===========================================
        %GUI
        data.spline1button = uicontrol(motionfig, 'style', 'radiobutton',...
            'value', 1, ...
            'string','1', ...
            'tag','splinegroup', ...
            'callback',[data.myname,' splinechoose'], ...
            'position', [100 0 50 20]);

        data.spline2button = uicontrol(motionfig, 'style', 'radiobutton',...
            'value', 0, ...
            'string','2', ...
            'tag','splinegroup', ...
            'callback',[data.myname,' splinechoose'], ...
            'position', [150 0 50 20]);

        data.spline3button = uicontrol(motionfig, 'style', 'radiobutton',...
            'value', 0, ...
            'string','3', ...
            'tag','splinegroup', ...
            'callback',[data.myname,' splinechoose'], ...
            'position', [200 0 50 20]);

        data.spline4button = uicontrol(motionfig, 'style', 'radiobutton',...
            'value', 0, ...
            'string','4', ...
            'tag','splinegroup', ...
            'callback',[data.myname,' splinechoose'], ...
            'position', [250 0 50 20]);

        data.filename = uicontrol(motionfig, 'style', 'edit',...
            'value', 0, ...
            'string','motionfile', ...
            'position', [470 0 100 20]);

        %make a control to stop the loop
        uicontrol(motionfig,'style','pushbutton',...
            'string','Quit', ...
            'position',[0 0 50 20], ...
            'callback',[data.myname,' quit']);

        %load button
        uicontrol(motionfig,'style','pushbutton',...
            'string','Load', ...
            'position',[320 0 50 20], ...
            'callback',[data.myname,' load']);

        %save button
        uicontrol(motionfig,'style','pushbutton',...
            'string','Save', ...
            'position',[370 0 50 20], ...
            'callback',[data.myname,' save']);

        data.splineIndex = 1; %always start with spline number 1
        %define spline variables
        data.tmax = 2 ; % seconds.
        data.NumControl = 25 ; %number of control points
        data.FPS = 30 ; %frames/sec
        data.ForceZeroSlope = 1 ;
        data.AngleRange = 100 ;

        data.t = linspace(0, data.tmax, data.NumControl) ;
        data.tt = linspace(0, data.tmax, data.tmax*data.FPS) ;
        %start with all zeros in control points
        data.numSplines = 4 ; %Four different spline curves
        data.y = zeros(length(data.t),data.numSplines);
        data.yy = zeros(length(data.tt),data.numSplines);

        if data.ForceZeroSlope
            cs = spline(data.t,[0; data.y(:,data.splineIndex); 0]);
        else
            cs = spline(data.t,data.y(:,data.splineIndex));
        end
        data.yy(:,data.splineIndex) = ppval(cs,data.tt)';

        hold on
        data.sp = plot(data.tt,data.yy(:,data.splineIndex));
        data.line1 = plot(data.t,data.y(:,data.splineIndex),'or');
        set(gca, 'ylim', [-data.AngleRange data.AngleRange])
        data.mousedown = 0; %init to no mouse click

        set(motionfig,'userdata',data);

        %the mouse-click sets a flag for point aquisition
        set(motionfig,'windowbuttondownfcn',...
            ['data=get(gcf,''userdata'');', ...
            'set(gcf,''windowbuttonmotionfcn'',[data.myname,'' update'']);']);
        set(motionfig,'windowbuttonupfcn',...
            'set(gcf,''windowbuttonmotionfcn'',[]);');

%
    case 'update'
        data = get(gcf,'userdata');

        %get the mouse position in graph units
        mouse = get(gca,'currentpoint');
        %calculate the point nearest to the
        %mouse click
        lineX = get(data.line1,'xdata');
        lineY = get(data.line1,'ydata')';
        [val, pnt] = min(abs(lineX-mouse(1,1)));

        %move the control point on the plot
        %and update control point data
        lineY(pnt) = mouse(1,2);
        set(data.line1, 'ydata', lineY);
        data.y(:,data.splineIndex) = lineY ;

        %make new spline
        if data.ForceZeroSlope
            cs = spline(data.t,[0; lineY; 0]);
        else
            cs = spline(data.t,lineY);
        end
        data.yy(:,data.splineIndex) = ppval(cs,data.tt)';
        set(data.sp, 'ydata', data.yy(:,data.splineIndex));
        set(gcf,'userdata',data);


    case 'splinechoose'
        data = get(gcf,'userdata');
        objhandle=findobj('tag','splinegroup');
        clrindex=find(objhandle ~= gco);
        set(objhandle(clrindex),'value',0);
        set(gco,'value',1);
        data.splineIndex = str2num(get(gco,'string'));
        hold off
        data.sp = plot(data.tt,data.yy(:,data.splineIndex));
        hold on
        data.line1 = plot(data.t,data.y(:,data.splineIndex),'or');
        set(gca, 'ylim', [-data.AngleRange data.AngleRange])
        set(gcf,'userdata',data);

    case 'load'
        data = get(gcf,'userdata');
        fname = [get(data.filename,'string'),'.mat'];
        pname = 'C:\MATLAB6p5\work';

        load(fullfile(pname, fname),'y','yy')
        data.y = y;
        data.yy = yy;
        hold off
        data.sp = plot(data.tt,data.yy(:,data.splineIndex));
        hold on
        data.line1 = plot(data.t,data.y(:,data.splineIndex),'or');
        set(gca, 'ylim', [-data.AngleRange data.AngleRange])
        set(gcf,'userdata',data);

    case 'save'
        data = get(gcf,'userdata');
        y = data.y;
        yy = data.yy;
        fname = [get(data.filename,'string'),'.mat'];
        pname = 'C:\MATLAB6p5\work';
        save(fullfile(pname, fname), 'y','yy')

    case 'quit'
        close(gcf)
end
% -------------------------------------------------------------------------------
% Bruce Land, Senior Research Assoc, NB&B and ECE
% W246 Mudd Hall, Cornell Univ, Ithaca, NY 14853
% 42/26.836North  76/28.744West
% 607-254-4346   FAX:-4308   BRL4@cornell.edu
% http://www.nbb.cornell.edu/neurobio/land/
% -------------------------------------------------------------------------------

