function [dat,hdr,chan_name,ev] = analyze_neuroscan

% Description:
%   This program is made under Matlab to analyze the eeg data from Neuroscan.
% 
% Syntax:
%   >> [dat,hdr,chan_name,ev] = analyze_neuroscan;
%
% Input:
%
% Output:
%   dat       - eeg data from Neuroscan in ascii format
%   hdr       - structured header information of eeg data
%               hdr.subject: subject's name
%               hdr.date: date of experiment
%               hdr.time: time of experiment
%               hdr.chan_no: number of channels
%               hdr.sr: sampling rate
%               hdr.type: data type in file ('continuous', 'epoch', etc)
%               hdr.rows
%               hdr.tp: eeg time points
%   chan_name - channel name
%   ev        - structured event information
%               ev.trial_no: sequential trial number
%               ev.ev_typr: user specified event type (ex. '1' for 'too', etc)
%               ev.toc: time of currence of each event
%
% Jeng-Ren Duann, CNL/Salk Inst, 2001-10-23

fname = input('File template (xxx.dat, xxx.ev2): ');

disp('read eeg data, header information, and event marks...');

ev_fname = [fname '.ev2'];
[ev,err] = get_event(ev_fname);
if err == -1,
  disp('Error in reading event markers, exit program!');
  return
end
dat_fname = [fname '.dat'];
[dat,hdr,chan_name,err] = get_data(dat_fname);
if err == -1,
  disp('Error in reading eeg data, exit program!');
  return
end
hdr.tp = length(dat)/hdr.chan_no;
dat = reshape(dat,hdr.chan_no,hdr.tp);

while 1,
  clc
  disp('Please input your selection ...');
  disp('  1, Inspect the raw eeg signal');
  disp('  2, Plottopo to compare eeg of events 1 and 2');
  disp('  3, ICA on event-based epochs');
  disp('  0, Quit');
  sprintf('\n\n');

  a = input('Choice: ');
  switch a
    case 1,
      eegplot(dat,250);
    case 2,
      frame1 = [];
      frame2 = [];
      for i=1:length(ev),
        if ev(i).ev_type == 1,
          frame1 = [frame1 ev(i).toc];
        end
        if ev(i).ev_type == 2,
          frame2 = [frame2 ev(i).toc];
        end
      end
      data1 = erpave(dat,frame1,500,-50,0,[1:50]);
      data2 = erpave(dat,frame2,500,-50,0,[1:50]);
      plottopo([data1 data2],'erik.locs',250);
    case 3,
      frame1 = [];
      frame2 = [];
      for i=1:length(ev),
        if ev(i).ev_type == 1,
          frame1 = [frame1 ev(i).toc];
        end
        if ev(i).ev_type == 2,
          frame2 = [frame2 ev(i).toc];
        end
      end
      data1 = erpave(dat,frame1,500,-50,0,[1:50],0,1); 
      data2 = erpave(dat,frame2,500,-50,0,[1:50],0,1);   
      
    case 0,
      break;
  end
end

%----------------------------------------------------
function [dat,hdr,chan_name,err] = get_data(filename)
%----------------------------------------------------
% Input:
%   filename - filename of the neuroscan eeg data (.dat)
% Output:
%   dat       - eeg data
%   hdr       - header information
%   chan_name - channel name
%   err       - error number

dat = [];
hdr = [];
chan_name = [];

fp = fopen(filename,'r');
if fp == -1,
  err = -1;
  return
end

% getting the header information
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
subject = a(i+1:length(a));
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
date = a(i+1:length(a));
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
time = a(i+1:length(a));
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
chan_no = str2num(a(i+1:length(a)));
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
sr = str2num(a(i+1:length(a)));
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
type = a(i+1:length(a));
a = fgets(fp);
for i=1:length(a),
  if a(i) == ']'
    break;
  end
end
rows = a(i+1:length(a));
hdr = struct(...
      'subject',subject,...
      'date',date,...
      'time',time,...
      'chan_no',chan_no,...
      'sr',sr,...
      'type',type,...
      'rows',rows);

% getting the channel name
fgets(fp);
a = fgets(fp);
x = findstr(a,'[');
y = findstr(a,']');
chan_name = repmat('        ',[chan_no 1]);
for i=1:chan_no,
  chan_name(i,:) = a(x(i)+1:y(i)-1);
end  

% getting the eeg data
fgets(fp);
fgets(fp);
fgets(fp);
fgets(fp);
fgets(fp);
dat = fscanf(fp,'%f');

fclose(fp);

err = 0;

%--------------------------------------
function [ev,err] = get_event(filename)
%--------------------------------------
% Input:
%   filename - file name of the neuroscan event marker (.ev2)
% Output:
%   ev  - structure for event {'trial_no','ev_type','toc'},
%         trial_no: sequential trial number,
%         ev_type: event type (ex. 1 for 'too', 2 for 'two', etc)
%         toc: time of currence for every event
%   err - error number

ev = struct(...
     'trial_no',[],...
     'ev_type',[],...
     'toc',[]);

fp = fopen(filename, 'r');
if fp == -1,
  err = -1;
  return
end
i = 1;
while ~feof(fp),
  a = fscanf(fp,'%d',1);
  if isempty(a),
    break;
  end
  ev(i).trial_no = a;
  a = fscanf(fp,'%d',1);
  ev(i).ev_type = a;
  fscanf(fp,'%d',2);
  fscanf(fp,'%f',1);
  a = fscanf(fp,'%d',1);
  ev(i).toc = a;
  i = i+1;
end
fclose(fp);

err = 0;