% 
%  MEG - MRI coordinate fit
%  
% 2008-3-12 Masa-aki Sato
% 2009-7-23 rhayashi modified to change file type.

Before using our batch program, 
please make following files.

1. Export Fastscan surface file (vrml_file) and stylus file (txt)
   For more detail, please look at manual
   (ExportingFastScan.doc)

2. run 'positioning' and make face file
   Registration of fiducial point on MRI and make sphere model
   1.Input subject name.
   2.Input mri file.(surface extract? choose NO!).
   3.Choose fiducial point.
   4.Make sphere model.
   5.Registration. 
   For more detail, please look at manual
   (SubjectRegistration.doc)
3.positioning program put face file into C:\subject_database\subjectName\
  If the directory does not exist, open $POSITIONING/conf/positioning.ini and 
  check the place of SubjectDatabase.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              MEG Positioning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

For batch MEG and MRI coordinate fitting, the following files are necessary:
    MRI analyze_file (.hdr/.img)
    face_file (.face.mat)
    vrml_file (.wrl)
    stylus_file (.txt)
    marker_files (.mrk)

There are 3 steps to register MEG sensor to MRI.

1. Fit FastScan face and MRI face and register marker on MRI
   If devise is EEG, the following process is not necessary.
2. Fit marker on MRI with MEG marker and get transform matrix from MEG to MRI
3. Transform MEG sensor position to MRI coordinate

To process several subject in batch mode, 
   define file list for each subject as sample file : test_batch_scanfit.m, 
                                                      test_batch_megfit.m
In this script, define following files
    MRI analyze_file (.hdr/.img)
    face_file (.face.mat)
    vrml_file (.wrl)
    stylus_file (.txt)
    marker_files (.mrk)
    fit_file   : output file name (.fit.mat)
    output_dir : output directory for pos.mat 

The following is actual procedure:

%
% --- Define file list for each subject as cell array 
%     in 'test_batch_scanfit', 'test_batch_megfit'

1. Fit FastScan face and MRI face and register marker on MRI

Run 'test_batch_scanfit'

The summary of fitting results is plotted 
and fitting error is printed on command screen. 

In addition, fitting results for each subject are saved as figure files 
('_hist.png','_slice.png','_face.png')

Please check the results carefully for each subject.
If fitting is not good, please ask us.

If devise is EEG, the following process is not necessary.

2. Fit marker on MRI with MEG marker and get transform matrix from MEG to MRI

Run 'test_batch_megfit'

The fitting results for each subject are saved as figure files 
('_megfit.png','_marker.png')

Please check the results carefully for each subject.
If fitting is not good, please ask us.

3. Transform MEG sensor position to MRI coordinate

If you have not imported MEG data, use 'job_meg'

If you have already imported MEG data, 
then, set list of '.meg.mat' , '.pos.mat' files
in 'test_trans_meg', and run it.

The fitting results for each meg file are saved as figure files 
(_meg.png)

Please check the results carefully for each meg file.
If fitting is not good, please ask us.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                              EEG Positioning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1. Export Fastscan surface file (vrml_file) and stylus file (txt)
   For more detail, please look at manual
   (ExportingFastScan.doc)

2. run 'positioning' and make face file
   Registration of fiducial point on MRI and make sphere model
   1.Input subject name.
   2.Input mri file.(surface extract? choose NO!).
   3.Choose fiducial point.
   4.Make sphere model.
   5.Registration. 
   For more detail, please look at manual
   (SubjectRegistration.doc)

For batch EEG and MRI coordinate fitting, the following files are necessary:
    MRI analyze_file (.hdr/.img)
    face_file (.face.mat)
    vrml_file (.wrl)
    stylus_file (.txt)
    label_file (.txt)  : please make yourself. 
                         List channel names as below.
                         The order of channel names should be same with stylus_file.
                         (Describe channel names except fiducial points)
                         e.g.
                           Fpz
                           Fp1
                           Fp2

There is only one step to register EEG sensor to MRI.

1. Fit FastScan face and MRI face to get transform matrix from EEG to MRI

To process several subject in batch mode, 
   define file list for each subject as sample file : test_batch_scanfit.m
In this script, define following files
    MRI analyze_file (.hdr/.img)
    face_file (.face.mat)
    vrml_file (.wrl)
    stylus_file (.txt)
    label_file (.mrk)
    fit_file   : output file name (.fit.mat)
    output_dir : output directory for .pos.mat 

The following is actual procedure:

%
% --- Define file list for each subject as cell array 
%     in 'test_batch_scanfit'

1. Fit FastScan face and MRI face and register marker on MRI

Run 'test_batch_scanfit'

The summary of fitting results is plotted 
and fitting error is printed on command screen. 

In addition, fitting results for each subject are saved as figure files 
('_hist.png','_slice.png','_face.png', '_sensor.png')

Please check the results carefully for each subject.
If fitting is not good, please ask us.

Put EEG sensor position into data file by using 'job_meg' function 
with .pos.mat file.
