Extract trials of file1 which are also included in trials of file2 : Intersection of trials in file1 and file2 --- Usage vb_extract_trial_cond(fname1,fname2,fout) --- Input fname1 : file name of examined trial fname2 : file name of condition trial fout : output file name of extracted trial --- Save variables status : status signal status_val(m) = status value for m-th condition (m=1:Ncomdition) trig(n) : Onset time index for n-th trial cond_id(n) : Condition ID for n-th trial ix_trial(:,n) : Time index for n-th trial [Tperiod x Ntrial] Tperiod : # of time sample in one trial Ntrial : # of trials parm : parameter setting parm.fsamp : Sample Frequency [Hz] parm.Pretrigger_ms : Pretrigger period [msec] parm.Posttrigger_ms: Posttrigger period [msec] 2009-6-14 Masa-aki Sato Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function vb_extract_trial_cond(fname1,fname2,fout) 0002 % Extract trials of file1 which are also included in trials of file2 0003 % : Intersection of trials in file1 and file2 0004 % --- Usage 0005 % vb_extract_trial_cond(fname1,fname2,fout) 0006 % --- Input 0007 % fname1 : file name of examined trial 0008 % fname2 : file name of condition trial 0009 % fout : output file name of extracted trial 0010 % --- Save variables 0011 % status : status signal 0012 % status_val(m) = status value for m-th condition (m=1:Ncomdition) 0013 % trig(n) : Onset time index for n-th trial 0014 % cond_id(n) : Condition ID for n-th trial 0015 % ix_trial(:,n) : Time index for n-th trial [Tperiod x Ntrial] 0016 % Tperiod : # of time sample in one trial 0017 % Ntrial : # of trials 0018 % parm : parameter setting 0019 % parm.fsamp : Sample Frequency [Hz] 0020 % parm.Pretrigger_ms : Pretrigger period [msec] 0021 % parm.Posttrigger_ms: Posttrigger period [msec] 0022 % 0023 % 2009-6-14 Masa-aki Sato 0024 % 0025 % Copyright (C) 2011, ATR All Rights Reserved. 0026 % License : New BSD License(see VBMEG_LICENSE.txt) 0027 0028 % load condition trial 0029 load(fname2,'ix_trial','trig','status','status_val','cond_id','parm'); 0030 0031 fs = parm.fsamp; 0032 ix_cond = ix_trial; 0033 Tmax = size(status,2); 0034 0035 % load main trial 0036 load(fname1,'ix_trial','trig','cond_id','status','status_val','parm'); 0037 0038 if fs ~= parm.fsamp, error('Sample frequency mismatch'); end; 0039 if Tmax ~= size(status,2), error('Sample length mismatch'); end 0040 0041 [T, Ntr] = size(ix_trial); 0042 0043 % Set condition flag 0044 flag = zeros(Tmax,1); 0045 0046 for n=1:size(ix_cond,2) 0047 flag(ix_cond(:,n)) = 1; 0048 end 0049 0050 % Extract trials included in 'ix_cond' 0051 nn = 0; 0052 nlist = []; 0053 ix_list = []; 0054 0055 for n=1:Ntr 0056 % old n-th trial 0057 jx = ix_trial(:,n); 0058 0059 % extract time points included in 'ix_cond' 0060 jj = find( flag(jx) > 0 ); 0061 Nj = length(jj); 0062 0063 if Nj > 0, 0064 nn = nn + 1; 0065 nlist = [nlist, n]; 0066 ix_list = [ix_list; jj(1) jj(end)]; 0067 end 0068 end 0069 0070 if nn == 0, 0071 ix_trial = []; 0072 trig = []; 0073 cond_id = []; 0074 return; 0075 end 0076 0077 % intersection of trials with 'ix_cond' 0078 j1 = max(ix_list(:,1)); 0079 j2 = min(ix_list(:,2)); 0080 0081 if j1 > j2, 0082 ix_trial = []; 0083 trig = []; 0084 cond_id = []; 0085 return; 0086 end 0087 0088 fprintf('# of extracted trials = %d\n',length(nlist)); 0089 0090 ix_trial = ix_trial( j1:j2 , nlist) ; 0091 trig = trig(nlist) ; 0092 cond_id = cond_id(nlist) ; 0093 0094 Pretrigger = trig(1) - ix_trial(1,1); 0095 Posttrigger= ix_trial(1,end) - trig(1); 0096 0097 parm.Pretrigger_ms = Pretrigger *(1000/fs); 0098 parm.Posttrigger_ms= Posttrigger*(1000/fs); 0099 0100 vb_fsave(fout,'ix_trial','trig','status','status_val','cond_id','parm');