Home > data-import > read_trial_info.m

read_trial_info

PURPOSE ^

read a trial info file and put it into .mat form

SYNOPSIS ^

function [] = read_trial_info(sub_id)

DESCRIPTION ^

 read a trial info file and put it into .mat form
 [] = read_trial_info(sub_id)

 Input:    
       fname - an info_file name
 Returns:
    A data structure trialInfo is created with the following fields
       camTime (1x1):    the synch time for camera
       speechTime (1x1): the synch time for Speech
       motionTime (1x1): the synch time for motion 
       camRate (1x1):    the recording rate of camera
       speechRate (1x1): the recording rate of speech 
       trials (2xn):     onset and offset frame numbers of n trials  

 Description : 
       This function reads the trial_info file and creates a new data structure and savse it in xxx_info.mat in each 
       subject folder.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [] = read_trial_info(sub_id)
0002 % read a trial info file and put it into .mat form
0003 % [] = read_trial_info(sub_id)
0004 %
0005 % Input:
0006 %       fname - an info_file name
0007 % Returns:
0008 %    A data structure trialInfo is created with the following fields
0009 %       camTime (1x1):    the synch time for camera
0010 %       speechTime (1x1): the synch time for Speech
0011 %       motionTime (1x1): the synch time for motion
0012 %       camRate (1x1):    the recording rate of camera
0013 %       speechRate (1x1): the recording rate of speech
0014 %       trials (2xn):     onset and offset frame numbers of n trials
0015 %
0016 % Description :
0017 %       This function reads the trial_info file and creates a new data structure and savse it in xxx_info.mat in each
0018 %       subject folder.
0019 %
0020 %
0021 if numel(sub_id) > 1
0022     for s = 1:numel(sub_id)
0023         read_trial_info(sub_id(s));
0024     end
0025     return
0026 end
0027 
0028 
0029 if isnumeric(sub_id)
0030     sub_dir = get_subject_dir(sub_id)
0031     names = dir(sprintf('%s/*_info.txt',sub_dir));
0032     fname = names(1).name
0033 else
0034     fname = sub_id
0035     sub_dir = fileparts(fname);
0036     if isempty(sub_dir)
0037         sub_dir = '.';
0038     end
0039     sub_dir
0040 end
0041 
0042 % open the trial_info text file
0043 fin = fopen(sprintf('%s/%s',sub_dir,fname),'r');
0044 if fin < 0
0045    error(['Could not open ',fname,' for input']);
0046 end
0047 
0048 % skip the header
0049 for i = 1 : 6
0050     tline = fgetl(fin);  
0051 end;
0052     
0053 % get the trial info
0054 for i = 1 : 20 % not more than 20 trial
0055     tline = fgetl(fin);
0056     if (size(tline,2) > 2) % not empty line
0057         list = strread(tline, '%d','delimiter',',');
0058         trials(list(1),:) = [list(2) list(3)];  
0059     else
0060         break;
0061     end;
0062     
0063 end;
0064 trialInfo.trials = trials;
0065 
0066 % get sensor timestamp
0067 for i = 1 : 3
0068     tline = fgetl(fin);  
0069 end;
0070 tline = fgetl(fin);
0071 list = strread(tline, '%d','delimiter',',');
0072 sensorHour = list(1);
0073 sensorMin = list(2);
0074 sensorSec = list(3);
0075 sensorMS = list(4);
0076 motionTime = sensorHour * 3600 + sensorMin * 60 + sensorSec + sensorMS/1000;
0077 
0078 % get speech timestamp
0079 for i = 1 : 4
0080     tline = fgetl(fin);  
0081 end;
0082 tline = fgetl(fin);
0083 list = strread(tline, '%d','delimiter',',');
0084 speechHour = list(1);
0085 speechMin = list(2);
0086 speechSec = list(3);
0087 speechMS = list(4);
0088 speechTime = speechHour * 3600 + speechMin * 60 + speechSec + speechMS/1000;
0089 trialInfo.speechRate = list(5);
0090 
0091 % get camera timestamp
0092 for i = 1 : 3
0093     tline = fgetl(fin);  
0094 end;
0095 tline = fgetl(fin);
0096 list = strread(tline, '%f','delimiter',',');
0097 camHour = list(1);
0098 camMin = list(2);
0099 camSec = list(3);
0100 camMS = list(4); 
0101 camTime = camHour * 3600 + camMin * 60 + camSec + camMS/1000;
0102 trialInfo.camRate = list(5);
0103 
0104 % close input file after reading
0105 fclose(fin);
0106 
0107 
0108 % calculate the overall offsets
0109 offset = 30; % 30 sec
0110 trialInfo.camTime = offset;
0111 trialInfo.speechTime = (speechTime - camTime) + offset;
0112 trialInfo.motionTime = (motionTime - camTime) + offset;
0113 if trials(1) == 0
0114     trialInfo.camCountsFromZero = 1;
0115 end
0116 
0117 
0118 % save the file
0119 fout = strrep(fname,'txt','mat');
0120 save(sprintf('%s/%s',sub_dir,fout), 'trialInfo');
0121 
0122 fout = sprintf('%s/derived/timing.mat',sub_dir);
0123 save(fout,'trialInfo');
0124 
0125 
0126 
0127

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005