function eodsim(fcn)
   global wavenumA;
   
   %sound,soundsc,wavwrite,wavread
   global wavetimeA;
   global waveA;
   global timeA;
   global s_rateA;
   global wavenumA;
   global realtimeA; %the length of the sound byte in realtime
   global inpfileA;
   global inpfileB;
   global finA;
   global finB;
   global wavenumB;
   global wavetimeB;
   global waveB;
   global timeB;
   global s_rateB;
   global wavenumB;
   global realtimeB; %the length of the sound byte in realtime
   global incstate; %determines if increment was called
   global inputfile;
   global fn;

   global equalzero;
   global number_entries;
   global spinumA;
   global spinumB;
   global pointone;
   global int_meanA;
   global sdA;
   global min_intervalA;
   
   %GLOBAL VARIABLES FOR SPIs
   global spitimeA;
   global spitimeB;
   global spimsA;
   global spimsB;
   
   %%%%%%STATE VARIABLES
   global Amade; %Sound matrices for A and B are good and made
   global Bmade;
   global bothmade;
   
%demo of user interface construction
%this function CALLS ITSELF via GUI callbacks

% By default make the GUI
%This code detects the first entry into the function
%from the command line with no parameters
if nargin == 0
   fcn = 'makeGUI';
end



%This is the main decision point of the function.
%The switch statement is executed once-per-fuction call
switch fcn
   
   %This code is executed ONCE when the function enters with
   %no arguments
case 'makeGUI'
   plotinfo.myname = mfilename;
   %%SET STATE VARIABLES TO FALSE
   incstate=0;
   Amade=0;
   Bmade=0;
   bothmade=0;
   format short;
   fig = figure('Position',centerfig(900,820),...
      'Resize','off',...
      'NumberTitle','off',...
      'Name','EOD Generator',...
      'Interruptible','off',...
      'Menubar','none',...
      'Color',get(0,'DefaultUIControlBackgroundColor'));
   
   %===Title text===================================
   uicontrol(gcf,'Style','text', ...
      'String','EOD Generator',...
      'fontsize',24, ...
      'HorizontalAlignment','Center',...
      'Position',[220,790,270,30],...
      'BackgroundColor',[0.8 1 0.8]);
   
   uicontrol(gcf,'Style','text', ...
      'String','by Joel Avrunin',...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[480,790,100,30],...
      'BackgroundColor',[0.8 1 0.8]);
   
   %%QUIT BUTTON - DO NOT MOVE
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','Quit',...
      'Position',[650,790,100,30],...
      'BackgroundColor',[0.8,0.8,0.8], ...
      'CallBack',[plotinfo.myname,' quit'] );
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','STOP',...
      'Position',[770,790,100,30],...
      'BackgroundColor',[0.8,0.8,0.8], ...
      'CallBack',[plotinfo.myname,' stopsound'] );

   
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','Play A & B',...
      'Position',[800,400,100,30],...
      'BackgroundColor',[0.8,0.8,0.8], ...
      'CallBack',[plotinfo.myname,' bothchannels'] );

   
  %%CHANNEL A BUTTONS
   %define the open waveform button
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','Open Waveform A',...
      'Position',[10,740,100,20],...
      'BackgroundColor',[0.8,0.8,0.8], ...
      'CallBack',[plotinfo.myname,' openmwvA'] );
   
    uicontrol(gcf,'Style','Pushbutton', ...
      'String','+',...
     'Position',[110,740,20,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' inceodA'] );
           
   
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','Artificial SPI',...
      'Position',[10,700,150,20],...
      'BackgroundColor',[0.8,0.8,0.8], ...
      'CallBack',[plotinfo.myname,' meantimeA'] );
  
    uicontrol(gcf,'Style','Pushbutton', ...
      'String','Open SPI A',...
     'Position',[10,640,100,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' openspiA'] );
  
 uicontrol(gcf,'Style','Pushbutton', ...
      'String','+',...
     'Position',[110,640,20,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' incspiA'] );
    
  uicontrol(gcf,'Style','Pushbutton', ...
      'String','Channel A Output',...
     'Position',[10,680,100,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' playwaveA'] );
   
  uicontrol(gcf,'Style','Pushbutton', ...
     'String','Channel A Test',...
     'Position',[10,660,100,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' playcontA'] );
  
 %Define fields for generating timebase on mean intervals
  uicontrol(gcf,'Style','text', ...
      'String','Interval Statistics - CH A',...
      'fontsize',10, ...
      'HorizontalAlignment','center',...
      'Position',[10,530,140,20],...
      'BackgroundColor',[0.52 0.90 0.97]);
   
   uicontrol('style','text',...
   'string','mean=',...
   'HorizontalAlignment','left',...
   'fontsize',10, ...
   'position',[10,510,100,20]);

  uicontrol('style','text',...
   'string','S.D.=',...
   'HorizontalAlignment','left',...
   'fontsize',10, ...
   'position',[10,490,100,20]);

  uicontrol('style','text',...
   'string','Min. Interval=',...
   'HorizontalAlignment','left',...
   'fontsize',10, ...
   'position',[10,470,100,20]);
   
   
   
   
   
   %CHANNEL B DEFINITIONS
   
   
  uicontrol(gcf,'Style','Pushbutton', ...
    'String','Open Waveform B',...
    'Position',[10,350,100,20],...
    'BackgroundColor',[0.8,0.8,0.8], ...
    'CallBack',[plotinfo.myname,' openmwvB'] );
   
  uicontrol(gcf,'Style','Pushbutton', ...
     'String','+',...
     'Position',[110,350,20,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' inceodB'] );
   
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','Artificial SPI',...
      'Position',[10,310,150,20],...
      'BackgroundColor',[0.8,0.8,0.8], ...
      'CallBack',[plotinfo.myname,' meantimeB'] );
   
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','Channel B Output',...
     'Position',[10,290,100,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' playwaveB'] );
   
  uicontrol(gcf,'Style','Pushbutton', ...
     'String','Channel B Test',...
     'Position',[10,270,100,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' playcontB'] );

  uicontrol(gcf,'Style','Pushbutton', ...
     'String','Open SPI B',...
     'Position',[10,250,100,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' openspiB'] );
  
   uicontrol(gcf,'Style','Pushbutton', ...
      'String','+',...
     'Position',[110,250,20,20],...
     'BackgroundColor',[0.8,0.8,0.8], ...
     'CallBack',[plotinfo.myname,' incspiB'] );
     
   uicontrol(gcf,'Style','text', ...
      'String','Interval Statistics - CH B',...
      'fontsize',10, ...
      'HorizontalAlignment','center',...
      'Position',[10,160,140,20],...
      'BackgroundColor',[0.52 0.90 0.97]);

   uicontrol('style','text',...
   'string','mean=',...
   'HorizontalAlignment','left',...
   'fontsize',10, ...
   'position',[10,140,100,20]);

  uicontrol('style','text',...
   'string','S.D.=',...
   'HorizontalAlignment','left',...
   'fontsize',10, ...
   'position',[10,120,100,20]);

  uicontrol('style','text',...
   'string','Min. Interval=',...
   'HorizontalAlignment','left',...
   'fontsize',10, ...
   'position',[10,100,100,20]);
   
   
  
  % =====Axes for Waveform
  %Channel A
   plotinfo.ax = axes('Units','pixels',...
      'Position',[590 490 296 270],...
      'Box','on',...
      'XLim',[-2.5 2],'YLim',[-1 1]);
   xlabel('t (msec)'); ylabel('v (mV)');
   
   %Channel B
   plotinfo.axB = axes('Units','pixels',...
      'Position',[590 100 296 270],...
      'Box','on',...
      'XLim',[-2.5 2],'YLim',[-1 1]);
   xlabel('t (msec)'); ylabel('v (mV)');

   
   % Axes for SPI
   %Channel A
   plotinfo.ax2 = axes('Units','pixels',...
      'Position',[200 490 325 270],...
      'Box','on',...
      'XLim',[0 10],'YLim',[-1 1]);
   xlabel('time (sec)');
   ylabel('SPI (msec)');
   
   %Channel B
   plotinfo.axB2 = axes('Units','pixels',...
      'Position',[200 100 325 270],...
      'Box','on',...
      'XLim',[0 10],'YLim',[-1 1]);
   xlabel('time (sec)');
   ylabel('SPI (msec)');

   set(fig,'UserData',plotinfo);
   
case 'openmwvA'
   plotinfo=get(gcf,'UserData');   
   Amade=0;
   bothmade=0;
   [fn pn]=uigetfile('f:\classes\research\*.eod','Open input file...');
													%opens file open dialog box;
													%fn=filename, pn=pathname
   inputfile = [pn fn];							%assigns variable inputfile to file name
   fid = fopen(inputfile, 'r');				%opens file, for 'r' read only
													%fid is an integer file identifier
   [version,count]=fread(fid,1,'char');	%reads binary data from opened file fid
													%writes it into matrix version, here '1'=1x1
                                       %precision is 'char'=8 bit
													%out argument count is #elements success read                                       
   if(count == 0)
	   fprintf (1,'%s %d\n', 'Error on read, end of file, count = ',count)
													%FID=1 (standard output); \n (linefeed)
      return;										%causes return to invoking function or keyboard
   end												%terminates scope of IF statement

   wavenumA=0;										%start with first EOD in file
   nchar_text=fread(fid,1,'short');			%size of text info (precis short=integer 16bit)
   temp=fread(fid,nchar_text,'char');		%integer representation of text info
   wave_text=sprintf('%c',temp);				%writes %c formatted data to string waveA_text
   n_bits=fread(fid,1,'char');				%?bits of vertical resolution?
   n_bytes=fread(fid,1,'char');				%?size of file?
   data_polarity=fread(fid,1,'char');		%determined by user at timeA of recording
   user_data=fread(fid,6,'float');			%six user defn, usu unused floating pnts 32bit
   s_rateA=fread(fid,1,'ulong');				%sampling rate of eod; unsigned long int 32 or 64 bit
   adrange=fread(fid,1,'float');				%?raw voltage range?
   n_pts=fread(fid,1,'long');					%number of points in eod sample
   waveA=fread(fid,n_pts,'short');			%eod voltage data
   timeA=linspace(0,1000*n_pts/s_rateA,n_pts);	%eod timeA data in milliseconds  
   %%%%%%%%%%
   %normeodp1.m
   %normalizes a wave to 0 baseline voltage
   %1.0 peak to peak voltage difference
   %and centers the time base on P1
   %%%%%%%%%%
   
   baseline = mean(waveA(1:20));
   waveA = waveA-baseline;
   [ymax,ivmax] = max(waveA);
   [ymin,ivmin] = min(waveA);
   waveA = waveA/(ymax-ymin);
   timeA = timeA - (timeA(ivmax));
   
      
   %%%%%%%%%%%%
   %%Resample the signal down to 44 KHz
   %%%%%%%%%%%%
   sfactor=s_rateA/1000;               %% drop the sampling rate by factor of 1000
   prate=44;                          %% This is the 44 KHz sampling rate required for sound card output
   qrate=sfactor;
   waveA=resample(waveA,prate,qrate);   %%resamples waveA by factor of p/q
   number_entries=size(waveA);         %%required to manually recalculate new timeAbase
   number_entries=number_entries(1);
   maxtime=size(timeA);
   maxtime=maxtime(2);
   accum_num=(timeA(maxtime)-timeA(1))/number_entries;
   
   %%calculate the new timeA spacing for resampled signal
   
   accum=timeA(1);
   timeA=[];
   
   for x=1:number_entries             %%loop creates new timeA matrix
      timeA=[timeA accum];
      accum=accum+accum_num;
   end;
   
   if timeA(1)<-2.5            %windows time from -2.5 ms to 2 ms
      for x=1:number_entries
         if timeA(x)<-2.5
            lowhold=x;         %essentially chews off any signal before 2.5 ms
         end;
         if timeA(x)<2
            highhold=x;
         end;
      end;
   
   
      %Truncate waveA into 4.5 ms frame
      waveA=waveA(lowhold:highhold);
      timeA=timeA(lowhold:highhold);
   end;
   newsize=size(waveA);
   newsize=newsize(1);
   
   waveA=waveA.*hanning(newsize);    %Apply hanning window to waveform
   
   bothmade=0;%set state variables to indicate new waveform is loaded
   Amade=0;   %these variables tell output function to recalculate WAV file

   sound_length=length(timeA);   %%Calculate realtime length of sound
   realtimeA=sound_length/44000;

   figure(1); %choose proper axes
 
   set(gcf,'currentaxes',plotinfo.ax) 
   plot(timeA,waveA);
   axis([-2.5 2 -1 1]);
   xlabel('t (msec)'); ylabel('v (mV)');
   title([fn '  ' num2str(wavenumA)],'fontsize',10);
   
   %output Wave information
   uicontrol(gcf,'Style','text', ...
      'String','WaveA:',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[0,430,750,20],...
      'BackgroundColor',[0.8 1 0.8]);
   
   uicontrol(gcf,'Style','text', ...
      'String',wave_text,...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[50,430,750,20],...
      'BackgroundColor',[0.8 1 0.8]);
   
   %this cleans up the error field if any exist from previous functions
    uicontrol(gcf,'Style','text', ...
      'String','',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[0.78 0.78 0.78]); 
      
   
   
case 'openmwvB'
   plotinfo=get(gcf,'UserData');   
   Bmade=0;
   bothmade=0;
   [fn pn]=uigetfile('f:\classes\research\*.eod','Open input file...');
													%opens file open dialog box;
													%fn=filename, pn=pathname
   inputfile = [pn fn];							%assigns variable inputfile to file name
   fid = fopen(inputfile, 'r');				%opens file, for 'r' read only
													%fid is an integer file identifier
   [version,count]=fread(fid,1,'char');	%reads binary data from opened file fid
													%writes it into matrix version, here '1'=1x1
                                       %precision is 'char'=8 bit
													%out argument count is #elements success read                                       
   if(count == 0)
	   fprintf (1,'%s %d\n', 'Error on read, end of file, count = ',count)
													%FID=1 (standard output); \n (linefeed)
      return;										%causes return to invoking function or keyboard
   end												%terminates scope of IF statement

   wavenumB=0;										%start with first EOD in file
   nchar_text=fread(fid,1,'short');			%size of text info (precis short=integer 16bit)
   temp=fread(fid,nchar_text,'char');		%integer representation of text info
   wave_text=sprintf('%c',temp);				%writes %c formatted data to string waveA_text
   n_bits=fread(fid,1,'char');				%?bits of vertical resolution?
   n_bytes=fread(fid,1,'char');				%?size of file?
   data_polarity=fread(fid,1,'char');		%determined by user at timeA of recording
   user_data=fread(fid,6,'float');			%six user defn, usu unused floating pnts 32bit
   s_rateB=fread(fid,1,'ulong');				%sampling rate of eod; unsigned long int 32 or 64 bit
   adrange=fread(fid,1,'float');				%?raw voltage range?
   n_pts=fread(fid,1,'long');					%number of points in eod sample
   waveB=fread(fid,n_pts,'short');			%eod voltage data
   timeB=linspace(0,1000*n_pts/s_rateB,n_pts);	%eod timeA data in milliseconds  
   %%%%%%%%%%
   %normeodp1.m
   %normalizes a wave to 0 baseline voltage
   %1.0 peak to peak voltage difference
   %and centers the time base on P1
   %%%%%%%%%%
   
   baseline = mean(waveB(1:20));
   waveB = waveB-baseline;
   [ymax,ivmax] = max(waveB);
   [ymin,ivmin] = min(waveB);
   waveB = waveB/(ymax-ymin);
   timeB = timeB - (timeB(ivmax));
   
   %%Resample the signal down to 44 KHz
   sfactor=s_rateB/1000;               %% drop the sampling rate by factor of 1000
   prate=44;                          %% This is the 44 KHz sampling rate required for sound card output
   qrate=sfactor;
   waveB=resample(waveB,prate,qrate);   %%resamples waveA by factor of p/q
   number_entries=size(waveB);         %%required to manually recalculate new timeAbase
   number_entries=number_entries(1);
   maxtime=size(timeB);
   maxtime=maxtime(2);
   accum_num=(timeB(maxtime)-timeB(1))/number_entries;
   
   %%calculate the new timeA spacing for resampled signal
   
   accum=timeB(1);
   timeB=[];
   
   for x=1:number_entries             %%loop creates new timeA matrix
      timeB=[timeB accum];
      accum=accum+accum_num;
   end;
   
   if timeB(1)<-2.5             %window from -2.5 ms to 2 ms
      for x=1:number_entries
         if timeB(x)<-2.5
            lowhold=x;
         end;
         if timeB(x)<2
            highhold=x;
         end;
      end;
   
   
      %Truncate waveA into 4 ms frame
      waveB=waveB(lowhold:highhold);
      timeB=timeB(lowhold:highhold);
   end;
   %%Calculate realtime length of sound
   sound_length=length(timeB);
   realtimeB=sound_length/44000;
   
   newsize=size(waveB);
   newsize=newsize(1);
   
   waveB=waveB.*hanning(newsize);    %Apply hanning window to waveform
   
   bothmade=0; %indicates that a new value has been read and WAVs must be recomputed
   Bmade=0;
   
   figure(1);
 
   set(gcf,'currentaxes',plotinfo.axB) 
   plot(timeB,waveB);
   axis([-2.5 2 -1 1]);
   xlabel('t (msec)'); ylabel('v (mV)');
   title([fn '  ' num2str(wavenumB)],'fontsize',10);
   
   uicontrol(gcf,'Style','text', ...
      'String','WaveB:',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[0,40,750,20],...
      'BackgroundColor',[0.8 1 0.8]);
   
   uicontrol(gcf,'Style','text', ...
      'String',wave_text,...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[50,40,750,20],...
      'BackgroundColor',[0.8 1 0.8]);
   
  %this cleans up the error field if any exist from previous functions
    uicontrol(gcf,'Style','text', ...
      'String','',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[0.78 0.78 0.78]); 
   
   
case 'meantimeA' %Generate based on mean, sd, and minimum interval
   plotinfo=get(gcf,'UserData');
   prompt={'Mean','Standard Deviation','Minimum Interval'};
   defans={'100','25','10'}; %defaults for blanks
   fields={'meanA','sdA','min_intervalA'};
   info=inputdlg(prompt,'Enter the timing in MILLISECONDS',1,defans);
      %create user input dialog box
   if ~isempty(info)
      info = cell2struct(info,fields);
      int_meanA = str2num(info.meanA);
      sdA = str2num(info.sdA);
      min_intervalA = str2num(info.min_intervalA);
   end

   uicontrol(gcf,'Style','text', ...
      'String',info.meanA,...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[95,510,45,20],...
      'BackgroundColor',[1 0.65 1]);
   uicontrol(gcf,'Style','text', ...
      'String',info.sdA,...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[95,490,45,20],...
      'BackgroundColor',[1 0.65 1]);
   uicontrol(gcf,'Style','text', ...
      'String',info.min_intervalA,...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[95,470,45,20],...
      'BackgroundColor',[1 0.65 1]);
   spi=[];
   time_accum=0;
   int_meanA=int_meanA/1000; %convert all millseconds to seconds
   min_intervalA=min_intervalA/1000;
   sdA=sdA/1000;
   while time_accum<60 %accumulate intervals until reaching 60 seconds
      interval=max(int_meanA+sdA*randn,min_intervalA);
      time_accum=time_accum+interval;
      spi=[spi;interval];
   end
   spimsA=spi*1000;
   spitimeA=cumsum(spi); %generate basetime on x-axis
  
   set(gcf,'currentaxes',plotinfo.ax2) ;
   cla;
   plot(spitimeA,spimsA,'k.');
   xlabel('Time (sec)');
   ylabel('SPI (msec)');
   axis([0 60 0 300]);
   
   uicontrol(gcf,'Style','text', ...
   'String','SPIA:',...
   'fontsize',12, ...
   'HorizontalAlignment','left',...
   'Position',[0,410,750,20],...
   'BackgroundColor',[0.8 1 0.8]);

   uicontrol(gcf,'Style','text', ...
   'String','Artificial waveform generated with above statistics',...
   'fontsize',12, ...
   'HorizontalAlignment','left',...
   'Position',[50,410,750,20],...
   'BackgroundColor',[0.8 1 0.8]);

   %this cleans up the error field if any exist from previous functions
    uicontrol(gcf,'Style','text', ...
      'String','',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[0.78 0.78 0.78]); 



case 'meantimeB' %Generate based on mean, sd, and minimum interval
   plotinfo=get(gcf,'UserData');
   prompt={'Mean','Standard Deviation','Minimum Interval'};
   defans={'100','25','10'};
   fields={'meanB','sdB','min_intervalB'};
   info=inputdlg(prompt,'Enter the timing in MILLISECONDS',1,defans);
   if ~isempty(info)
      info = cell2struct(info,fields);
      int_meanB = str2num(info.meanB);
      sdB = str2num(info.sdB);
      min_intervalB = str2num(info.min_intervalB);
   end
   
   uicontrol(gcf,'Style','text', ...
      'String',info.meanB,...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[95,140,45,20],...
      'BackgroundColor',[1 0.65 1]);
   uicontrol(gcf,'Style','text', ...
      'String',info.sdB,...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[95,120,45,20],...
      'BackgroundColor',[1 0.65 1]);
   uicontrol(gcf,'Style','text', ...
      'String',info.min_intervalB,...
      'fontsize',12, ...
      'HorizontalAlignment','Center',...
      'Position',[95,100,45,20],...
      'BackgroundColor',[1 0.65 1]);
   spi=[];
   time_accum=0;
   int_meanB=int_meanB/1000; %convert all millseconds to seconds
   min_intervalB=min_intervalB/1000;
   sdB=sdB/1000;
   while time_accum<60
      interval=max(int_meanB+sdB*randn,min_intervalB);
      time_accum=time_accum+interval;
      spi=[spi;interval];
   end
   spimsB=spi*1000;
   spitimeB=cumsum(spi);
  
   set(gcf,'currentaxes',plotinfo.axB2) ;
   cla;
   plot(spitimeB,spimsB,'k.');
   xlabel('Time (sec)');
   ylabel('SPI (msec)');
   axis([0 60 0 300]);

uicontrol(gcf,'Style','text', ...
      'String','SPIB:',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[0,20,750,20],...
      'BackgroundColor',[0.8 1 0.8]);

uicontrol(gcf,'Style','text', ...
      'String','Artificial waveform generated with above statistics',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[50,20,750,20],...
      'BackgroundColor',[0.8 1 0.8]);
   
uicontrol(gcf,'Style','text', ...
      'String','',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[0.78 0.78 0.78]); 
   
   
case 'quit'
   fig = gcf;
   quit_reply = questdlg('Really quit EODSIM?');
   if strcmp(quit_reply,'Yes')
      close(fig);
   end
   
   %both of these functions just allow a quick preview of the waveform without
   %needing to create full sound files with the SPIs
case 'playcontA'
   pointone=zeros(1,4400);
   attr = struct('async',{0},'channels',{2},'scaling',{'auto'},'flow',{'auto'},'count',{1});
   for j=1:10
      wavplay(waveA,44000,attr);
      wavplay(pointone,44000);
   end
   
case 'playcontB'
   pointone=zeros(1,4400);
   attr = struct('async',{0},'channels',{3},'scaling',{'auto'},'flow',{'auto'},'count',{1});
   for j=1:10
      wavplay(waveA,44000,attr);
      wavplay(pointone,44000);
   end
   
case 'bothchannels' %outputs on both channels
     %this code will display if the program crashes while trying to play the sound due to a memory overflow
     uicontrol(gcf,'Style','text', ...
      'String','ERROR - OUT OF MEMORY',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);
   if bothmade==0

      numpntsA=size(spimsA); %find out how many SPI points there are
      numpntsB=size(spimsB);
      numpntsA=numpntsA(1);
      numpntsB=numpntsB(1);
      outwaveA=[];
      outwaveB=[];      
      n=1;     
   while (n<numpntsA)
      current_interval=spimsA(n)*10;%number of 0.0001 intervals required
      sampled_interval=current_interval*4.4; %there are 4.4 points in 0.1 msec
      sampled_interval=round(sampled_interval);   %chop off decimal--possible error of up to 0.01 msec per interval
      zero_matrix=zeros(sampled_interval,1);
      outwaveA=[outwaveA;zero_matrix;waveA];
      n=n+1;
      if spitimeA(n)>=10 %cut sound at 10 seconds due to memory problems
         newcount=n;
         n=numpntsA;
      end
   end
   n=1;
   while (n<numpntsB)
      current_interval=spimsB(n)*10;%number of 0.0001 intervals required
      sampled_interval=current_interval*4.4; %there are 4.4 points in 0.1 msec
      sampled_interval=round(sampled_interval);   %chop off decimal--possible error of up to 0.01 msec per interval
      zero_matrix=zeros(sampled_interval,1);
      outwaveB=[outwaveB;zero_matrix;waveB];
      n=n+1;
      if spitimeB(n)>=10 %cut sound at 10 seconds due to memory problems
         newcount=n;
         n=numpntsB;
      end
   end
   numpntsA=size(outwaveA); %find out how many SPI points there are
   numpntsB=size(outwaveB);
   numpntsA=numpntsA(1);
   numpntsB=numpntsB(1);
   %equalize the two different waveforms with zero matrices.  This prevents mismatch in the stereo output
   if (numpntsA<numpntsB)
      point_difference=numpntsB-numpntsA;
      zero_matrix=zeros(point_difference,1);
      outwaveA=[outwaveA;zero_matrix];
   end
   if (numpntsB<numpntsA)
      point_difference=numpntsA-numpntsB;
      zero_matrix=zeros(point_difference,1);
      outwaveB=[outwaveB;zero_matrix];
   end
   bothmade=1;    %indicate that waveform is created
     
   %linear ramping of output to not startle fish
   linear_accum=0;
   for i=1:88e3 %two seconds of waveform at 44KHz
      outwaveA(i)=outwaveA(i)*linear_accum;
      outwaveB(i)=outwaveB(i)*linear_accum;
      linear_accum=linear_accum+11.36e-6; %creates linear ramp
   end
   
   outwaveBOTH=[outwaveA outwaveB];
   wavwrite(outwaveBOTH,44000,'eod-both.wav');   
end

   uicontrol(gcf,'Style','text', ...
      'String','Sound is currently playing...',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);


attr = struct('async',{1},'channels',{0},'scaling',{'auto'},'flow',{'auto'},'count',{1});
wavplay('eod-both.wav',attr);



case 'playwaveA' %outputs the wave
   
     uicontrol(gcf,'Style','text', ...
      'String','ERROR - OUT OF MEMORY',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);
   if Amade==0 %check if necessary to make WAV file

      numpntsA=size(spimsA); %find out how many SPI points there are
      numpntsA=numpntsA(1);
      outwaveA=[];
      n=1;      
   while (n<numpntsA)
      current_interval=spimsA(n)*10;%number of 0.0001 intervals required
      sampled_interval=current_interval*4.4; %there are 4.4 points in 0.1 msec
      sampled_interval=round(sampled_interval);   %chop off decimal--possible error of up to 0.01 msec per interval
      zero_matrix=zeros(sampled_interval,1);
      outwaveA=[outwaveA;zero_matrix;waveA];
      n=n+1;
      if spitimeA(n)>=20    %due to memory shortage, we end wave at 20 seconds
         n=numpntsA;
      end
   end
   %linear ramping of output to not startle fish
   linear_accum=0;
   for i=1:88e3 %two seconds of waveform
      outwaveA(i)=outwaveA(i)*linear_accum;
      linear_accum=linear_accum+11.36e-6; %creates linear ramp
   end
   Amade=1;
   wavwrite(outwaveA,44000,'channelA.wav');   
end

   uicontrol(gcf,'Style','text', ...
      'String','Sound is currently playing...',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);

attr = struct('async',{1},'channels',{0},'scaling',{'auto'},'flow',{'auto'},'count',{1});
wavplay('channelA.wav',attr);

   
case 'playwaveB' %outputs the wave
   uicontrol(gcf,'Style','text', ...
      'String','ERROR - OUT OF MEMORY',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);
   if Bmade==0

      numpntsB=size(spimsB); %find out how many SPI points there are
      numpntsB=numpntsB(1);
      outwaveB=[];
      n=1;      
   while (n<numpntsB)
      current_interval=spimsB(n)*10;%number of 0.0001 intervals required
      sampled_interval=current_interval*4.4; %there are 4.4 points in 0.1 msec
      sampled_interval=round(sampled_interval);   %chop off decimal--possible error of up to 0.01 msec per interval
      zero_matrix=zeros(sampled_interval,1);
      outwaveB=[outwaveB;zero_matrix;waveB];
      n=n+1;
      if spitimeB(n)>=20     %due to memory shortage, we end wave at 20 seconds
         n=numpntsB;
      end
   end

   %linear ramping of output to not startle fish
   linear_accum=0;
   for i=1:88e3 %two seconds of waveform
      outwaveB(i)=outwaveB(i)*linear_accum;
      linear_accum=linear_accum+11.36e-6; %creates linear ramp
   end

   Bmade=1;
   wavwrite(outwaveB,44000,'channelB.wav');   
end

   uicontrol(gcf,'Style','text', ...
      'String','Sound is currently playing...',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);

attr = struct('async',{1},'channels',{0},'scaling',{'auto'},'flow',{'auto'},'count',{1});
wavplay('channelB.wav',attr);

   
case 'openspiA'
      plotinfo=get(gcf,'UserData');
      Amade=0; %must reconstruct sound now
      bothmade=0;
      
      [fn,pn]=uigetfile('E:\MATLAB\toolbox\Mwv\playback\*.spi','Open input file...');
      spinumA = -1;  
      inpfileA = [pn fn];
      finA = fopen(inpfileA, 'r');            %read only
      spinumA=spinumA+1;                       %start at the first spi in the file; name it number '0'
      [version,count]=fread(finA,1,'char');
      if count == 0
         uicontrol(gcf,'Style','text', ...
            'String','Error: End of SPI file on Wave A',...
            'fontsize',12, ...
            'HorizontalAlignment','left',...
            'Position',[10,390,750,20],...
            'BackgroundColor',[1 0.2 0]);   
         return;
      end
      %
      %Standard variables (or fields) recorded in an SPI (historically, raster) file
      %
      nchar_text=fread(finA,1,'short');      %size of text info that follows
      temp=fread(finA,nchar_text,'char');    %integer representation of text info (assign to var temp)
      ras_text=sprintf('%c',temp);          %writes %c formatted text to string ras_text
      %as w/ EODs, display this text in the GUI plots
      uicontrol(gcf,'Style','text', ...
         'String','SPIA:',...
         'fontsize',12, ...
         'HorizontalAlignment','left',...
         'Position',[0,410,750,20],...
         'BackgroundColor',[0.8 1 0.8]);
      uicontrol(gcf,'Style','text', ...
         'String',ras_text,...
         'fontsize',12, ...
         'HorizontalAlignment','left',...
         'Position',[50,410,750,20],...
         'BackgroundColor',[0.8 1 0.8]);
      user=fread(finA,3,'float');            %three user-defined floating point elements (ignore for SPIs)
      spi_rate=fread(finA,1,'ulong');        %sampling rate of SPI (in Hz)
      n_lines=fread(finA,1,'ulong');         %number of lines in raster (ignore for SPIs)
      n_pts=fread(finA,1,'ulong');           %number of points in entire SPI or raster
                                      %important for reading in correct # SPI points (below)
      width=fread(finA,1,'ulong');           %width of a single line in the raster so that points past
                                      %edge can be dropped from plots (ignore for SPIs)
      ras=fread(finA,n_pts,'ulong');         %ras is a vector of the entire raster (all 'lines' combined)
                                      %i.e., a vector of number of clock ticks that accumulate
                                      %between spikes or EODs
%compute and plot SPI from raster file data
      spi=ras/spi_rate;                  %SPI (in seconds, rather than clock ticks)
      %correct if intervals less than 4.5 ms are detected
      num_spi=size(spi);
      num_spi=num_spi(1);
      temp_flag=0;
      for i=1:num_spi
         if spi(i)<0.0045
            spi(i)=0.0045
            temp_flag=1;
            uicontrol(gcf,'Style','text', ...
               'String','Error: one or more interpulse intervals less than 4.5 ms detected.  Corrected to 4.5 minimum.',...
               'fontsize',12, ...
               'HorizontalAlignment','left',...
               'Position',[10,390,750,20],...
               'BackgroundColor',[0.78 0.78 0.78]); 
         end
      end
      spimsA=spi*1000;                    %SPI (in milliseconds)
      temp_min=min(spimsA);               %calculate SPI stats
      temp_sd=std(spimsA);
      temp_mean=mean(spimsA);
      uicontrol(gcf,'Style','text', ...
         'String',temp_mean,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,510,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_sd,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,490,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_min,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,470,45,20],...
         'BackgroundColor',[1 0.65 1]); 
      spitimeA=cumsum(spi);                  %cumulative sum of SPIs to reconstruct time axis (in sec)
      set(gcf,'currentaxes',plotinfo.ax2) ;
      plot(spitimeA,spimsA,'k.');
      xlabel('Time (sec)');
      ylabel('SPI (msec)');
      axis([0 60 0 300]);                %scale the GUI windows (for SPIs) something like this
      bothmade=0;
      Amade=0;
      %clear the error message if it is still there
      if temp_flag==0 %if no other error message, clean up error field
         uicontrol(gcf,'Style','text', ...
            'String','',...
            'fontsize',12, ...
            'HorizontalAlignment','left',...
            'Position',[10,390,750,20],...
            'BackgroundColor',[0.78 0.78 0.78]); 
      end
   
case 'openspiB'
   plotinfo=get(gcf,'UserData');
   Bmade=0; %must reconstruct sound now
   bothmade=0;
   [fn,pn]=uigetfile('E:\MATLAB\toolbox\Mwv\playback\*.spi','Open input file...');
   inpfile = [pn fn];
   finB = fopen(inpfile, 'r');            %read only
   spinumB = 0;                           %start at the first spi in the file; name it number '0'
   [version,count]=fread(finB,1,'char');
   if count == 0
   uicontrol(gcf,'Style','text', ...
      'String','Error: End of SPI file on Wave B',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);   
   return;
end
%Standard variables (or fields) recorded in an SPI (historically, raster) file
    nchar_text=fread(finB,1,'short');      %size of text info that follows
    temp=fread(finB,nchar_text,'char');    %integer representation of text info (assign to var temp)
    ras_text=sprintf('%c',temp);          %writes %c formatted text to string ras_text
    %as w/ EODs, display this text in the GUI plots
    uicontrol(gcf,'Style','text', ...
       'String','SPIB:',...
       'fontsize',12, ...
       'HorizontalAlignment','left',...
       'Position',[0,20,750,20],...
       'BackgroundColor',[0.8 1 0.8]);
    uicontrol(gcf,'Style','text', ...
       'String',ras_text,...
       'fontsize',12, ...
       'HorizontalAlignment','left',...
       'Position',[50,20,750,20],...
       'BackgroundColor',[0.8 1 0.8]);
    
    
    user=fread(finB,3,'float');            %three user-defined floating point elements (ignore for SPIs)
    spi_rate=fread(finB,1,'ulong');        %sampling rate of SPI (in Hz)
    n_lines=fread(finB,1,'ulong');         %number of lines in raster (ignore for SPIs)
    n_pts=fread(finB,1,'ulong');           %number of points in entire SPI or raster
    %important for reading in correct # SPI points (below)
    width=fread(finB,1,'ulong');           %width of a single line in the raster so that points past
    %edge can be dropped from plots (ignore for SPIs)
    ras=fread(finB,n_pts,'ulong');         %ras is a vector of the entire raster (all 'lines' combined)
    %i.e., a vector of number of clock ticks that accumulate
    %between spikes or EODs
    %
    %Now compute and plot SPI from raster file data
    %
    spi=ras/spi_rate;                  %SPI (in seconds, rather than clock ticks)
    %correct if intervals less than 4.5 ms are detected
      num_spi=size(spi);
      num_spi=num_spi(1);
      temp_flag=0;
      for i=1:num_spi
         if spi(i)<0.0045
            spi(i)=0.0045
            temp_flag=1;
            uicontrol(gcf,'Style','text', ...
               'String','Error: one or more interpulse intervals less than 4.5 ms detected.  Corrected to 4.5 minimum.',...
               'fontsize',12, ...
               'HorizontalAlignment','left',...
               'Position',[10,390,750,20],...
               'BackgroundColor',[0.78 0.78 0.78]); 
         end
      end
        
      spimsB=spi*1000;                    %SPI (in milliseconds)
      temp_min=min(spimsB);               %calculate SPI stats
      temp_sd=std(spimsB);
      temp_mean=mean(spimsB);
      uicontrol(gcf,'Style','text', ...
         'String',temp_mean,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,140,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_sd,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,120,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_min,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,100,45,20],...
         'BackgroundColor',[1 0.65 1]); 

    spitimeB=cumsum(spi);                  %cumulative sum of SPIs to reconstruct time axis (in sec)
    set(gcf,'currentaxes',plotinfo.axB2) ;
    plot(spitimeB,spimsB,'k.');
    xlabel('Time (sec)');
    ylabel('SPI (msec)');
    axis([0 60 0 300]);                %scale the GUI windows (for SPIs) something like this
    
    bothmade=0;
    Bmade=0;
    %clear the error message if it is still there
    if temp_flag==0 %if no other error message, clean up error field
       
       uicontrol(gcf,'Style','text', ...
          'String','',...
          'fontsize',12, ...
          'HorizontalAlignment','left',...
          'Position',[10,390,750,20],...
          'BackgroundColor',[0.78 0.78 0.78]);
    end

case 'incspiA'
      plotinfo=get(gcf,'UserData');
      Amade=0; %must reconstruct sound now
      bothmade=0;
      
      %essentially repeates OpenSPI, but keeps original file open
      spinumA=spinumA+1;                       %start at the first spi in the file; name it number '0'
      [version,count]=fread(finA,1,'char');
      if count == 0
         uicontrol(gcf,'Style','text', ...
            'String','Error: End of SPI file on Wave A',...
            'fontsize',12, ...
            'HorizontalAlignment','left',...
            'Position',[10,390,750,20],...
            'BackgroundColor',[1 0.2 0]);   
         return;
      end
      %
      %Standard variables (or fields) recorded in an SPI (historically, raster) file
      nchar_text=fread(finA,1,'short');      %size of text info that follows
      temp=fread(finA,nchar_text,'char');    %integer representation of text info (assign to var temp)
      ras_text=sprintf('%c',temp);          %writes %c formatted text to string ras_text
      %as w/ EODs, display this text in the GUI plots
      
      uicontrol(gcf,'Style','text', ...
         'String','SPIA:',...
         'fontsize',12, ...
         'HorizontalAlignment','left',...
         'Position',[0,410,750,20],...
         'BackgroundColor',[0.8 1 0.8]);
      uicontrol(gcf,'Style','text', ...
         'String',ras_text,...
         'fontsize',12, ...
         'HorizontalAlignment','left',...
         'Position',[50,410,750,20],...
         'BackgroundColor',[0.8 1 0.8]);
      user=fread(finA,3,'float');            %three user-defined floating point elements (ignore for SPIs)
      spi_rate=fread(finA,1,'ulong');        %sampling rate of SPI (in Hz)
      n_lines=fread(finA,1,'ulong');         %number of lines in raster (ignore for SPIs)
      n_pts=fread(finA,1,'ulong');           %number of points in entire SPI or raster
      %important for reading in correct # SPI points (below)
      width=fread(finA,1,'ulong');           %width of a single line in the raster so that points past
      %edge can be dropped from plots (ignore for SPIs)
      ras=fread(finA,n_pts,'ulong');         %ras is a vector of the entire raster (all 'lines' combined)
      %i.e., a vector of number of clock ticks that accumulate
      %between spikes or EODs
      %
      %Now compute and plot SPI from raster file data
      %
      
      spi=ras/spi_rate;                  %SPI (in seconds, rather than clock ticks)
           %correct if intervals less than 4.5 ms are detected
      num_spi=size(spi);
      num_spi=num_spi(1);
      temp_flag=0;
      for i=1:num_spi
         if spi(i)<0.0045
            spi(i)=0.0045
            temp_flag=1;
            uicontrol(gcf,'Style','text', ...
               'String','Error: one or more interpulse intervals less than 4.5 ms detected.  Corrected to 4.5 minimum.',...
               'fontsize',12, ...
               'HorizontalAlignment','left',...
               'Position',[10,390,750,20],...
               'BackgroundColor',[0.78 0.78 0.78]); 
         end
      end      
      spimsA=spi*1000;                    %SPI (in milliseconds)
      temp_min=min(spimsA);               %calculate SPI stats
      temp_sd=std(spimsA);
      temp_mean=mean(spimsA);
      uicontrol(gcf,'Style','text', ...
         'String',temp_mean,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,510,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_sd,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,490,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_min,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,470,45,20],...
         'BackgroundColor',[1 0.65 1]); 

      spitimeA=cumsum(spi);                  %cumulative sum of SPIs to reconstruct time axis (in sec)
      set(gcf,'currentaxes',plotinfo.ax2) ;
      plot(spitimeA,spimsA,'k.');
      xlabel('Time (sec)');
      ylabel('SPI (msec)');
      axis([0 60 0 300]);                %scale the GUI windows (for SPIs) something like this
      
      bothmade=0;
      Amade=0;      
      
   case 'incspiB'
      plotinfo=get(gcf,'UserData');
      Bmade=0; %must reconstruct sound now
      bothmade=0;
      spinumB=spinumB+1;                       %start at the first spi in the file; name it number '0'
      [version,count]=fread(finB,1,'char');
      if count == 0
     uicontrol(gcf,'Style','text', ...
        'String','Error: End of SPI file on Wave B',...
        'fontsize',12, ...
        'HorizontalAlignment','left',...
        'Position',[10,390,750,20],...
        'BackgroundColor',[1 0.2 0]);   
     return;
  end
  %
  %Standard variables (or fields) recorded in an SPI (historically, raster) file
  %
  nchar_text=fread(finB,1,'short');      %size of text info that follows
  temp=fread(finB,nchar_text,'char');    %integer representation of text info (assign to var temp)
  ras_text=sprintf('%c',temp);          %writes %c formatted text to string ras_text
  %as w/ EODs, display this text in the GUI plots
  
  uicontrol(gcf,'Style','text', ...
     'String','SPIB:',...
     'fontsize',12, ...
     'HorizontalAlignment','left',...
     'Position',[0,20,750,20],...
     'BackgroundColor',[0.8 1 0.8]);
  
  uicontrol(gcf,'Style','text', ...
     'String',ras_text,...
     'fontsize',12, ...
     'HorizontalAlignment','left',...
     'Position',[50,20,750,20],...
     'BackgroundColor',[0.8 1 0.8]);
  
  
  user=fread(finB,3,'float');            %three user-defined floating point elements (ignore for SPIs)
  spi_rate=fread(finB,1,'ulong');        %sampling rate of SPI (in Hz)
  n_lines=fread(finB,1,'ulong');         %number of lines in raster (ignore for SPIs)
  n_pts=fread(finB,1,'ulong');           %number of points in entire SPI or raster
  %important for reading in correct # SPI points (below)
  width=fread(finB,1,'ulong');           %width of a single line in the raster so that points past
  %edge can be dropped from plots (ignore for SPIs)
  ras=fread(finB,n_pts,'ulong');         %ras is a vector of the entire raster (all 'lines' combined)
  %i.e., a vector of number of clock ticks that accumulate
  %between spikes or EODs
  %
  %Now compute and plot SPI from raster file data
  %
  spi=ras/spi_rate;                  %SPI (in seconds, rather than clock ticks)
  %correct if intervals less than 4.5 ms are detected
  num_spi=size(spi);
  num_spi=num_spi(1);
  temp_flag=0;
      for i=1:num_spi
         if spi(i)<0.0045
            spi(i)=0.0045
            temp_flag=1;
            uicontrol(gcf,'Style','text', ...
               'String','Error: one or more interpulse intervals less than 4.5 ms detected.  Corrected to 4.5 minimum.',...
               'fontsize',12, ...
               'HorizontalAlignment','left',...
               'Position',[10,390,750,20],...
               'BackgroundColor',[0.78 0.78 0.78]); 
         end
      end
      
      spimsB=spi*1000;                    %SPI (in milliseconds)
      temp_min=min(spimsB);               %calculate SPI stats
      temp_sd=std(spimsB);
      temp_mean=mean(spimsB);
      uicontrol(gcf,'Style','text', ...
         'String',temp_mean,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,140,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_sd,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,120,45,20],...
         'BackgroundColor',[1 0.65 1]);
      uicontrol(gcf,'Style','text', ...
         'String',temp_min,...
         'fontsize',12, ...
         'HorizontalAlignment','Center',...
         'Position',[95,100,45,20],...
         'BackgroundColor',[1 0.65 1]); 

      spitimeB=cumsum(spi);                  %cumulative sum of SPIs to reconstruct time axis (in sec)
      set(gcf,'currentaxes',plotinfo.axB2) ;
      plot(spitimeB,spimsB,'k.');
      xlabel('Time (sec)');
      ylabel('SPI (msec)');
      axis([0 60 0 300]);                %scale the GUI windows (for SPIs) something like this
      
      bothmade=0;
      Bmade=0;
      
case 'stopsound' %simple function to stop currently playing sound
      uicontrol(gcf,'Style','text', ...
      'String','Sound is stopped!',...
      'fontsize',12, ...
      'HorizontalAlignment','left',...
      'Position',[10,390,750,20],...
      'BackgroundColor',[1 0.2 0]);
   wavplay('stop')
   
end
return

%===A utility to center the window on the screen============
function pos = centerfig(width,height)
   % Find the screen size in pixels
   screen_s = get(0,'ScreenSize');
   pos = [screen_s(3)/2 - width/2, screen_s(4)/2 - height/2, width, height];
return
