function list = ctx2mat(fname) % function list = ctx2mat(fname) % Given a string fname, read in that Cortex data file and produce a full % representation of that information in a Matlab list. % % The top-level list structure is interpretable as: % { trial1, trial2, ..., trialn } % % Each trial is itself a list containing: % { header, encode_times, encode_number, eog_values, epp_values } % % The header is a vector of: % [ cond_no, reapeat_no, block_no, trial_no, isi_size, code_size, ... % eog_size, epp_size, khz_res, eye_storage_rate, expected, response,... % response_err ] % For an interpreation of these fields, refer to the Cortex documentation. % These variables are shown in the same order as they appear in the Cortex % header. % % Version 1.0 % For additional code, updated versions, questions or comments, please refer % to the Active Perception Lab home page % http://www.cnbc.cmu.edu/~tai/lab.html % Copyright 1998-1999 % Rick Romero % rickr+@cmu.edu % Permission to use and modify is granted, % but not to redistribute original or modified code. fid = fopen(fname, 'r'); if (fid < 1) sprintf('Problem with opening file %s', fname) return end shift1 = 256; shift2 = shift1 * shift1; shift3 = shift1 * shift2; entry = 1; list = {}; [header,c] = fread(fid, [26,1], 'uchar'); while (c == 26) i = 1; length = header(i) + header(i+1)*shift1; i = i + 2; cond_no = header(i) + header(i+1)*shift1; i = i + 2; repeat_no = header(i) + header(i+1)*shift1; i = i + 2; block_no = header(i) + header(i+1)*shift1; i = i + 2; trial_no = header(i) + header(i+1)*shift1; i = i + 2; isi_size = header(i) + header(i+1)*shift1; i = i + 2; isi_size = isi_size / 4; code_size = header(i) + header(i+1)*shift1; i = i + 2; code_size = code_size / 2; eog_size = header(i) + header(i+1)*shift1; i = i + 2; eog_size = eog_size / 2; epp_size = header(i) + header(i+1)*shift1; i = i + 2; epp_size = epp_size / 2; khz_res = header(i); i = i + 1; eye_storage_rate = header(i); i = i + 1; expected = header(i) + header(i+1)*shift1; i = i + 2; response = header(i) + header(i+1)*shift1; i = i + 2; response_err = header(i) + header(i+1)*shift1; curEntry = {[cond_no,repeat_no,block_no,trial_no,isi_size,code_size,eog_size,epp_size,khz_res,eye_storage_rate,expected,response,response_err],[],[],[],[]}; [curEntry{1}, ftell(fid)]; if (isi_size > 0) [curEnc,cnt] = fread(fid, [4,isi_size], 'uchar'); if (cnt ~= 4*isi_size) error(sprintf('Problem reading ISI codes:%d', ftell(fid))); end curEnc = curEnc(1,:) + curEnc(2,:) * shift1 + curEnc(3,:) * shift2 + ... curEnc(4,:) * shift3; curEntry{2} = curEnc; end if (code_size > 0) [curCodes,cnt] = fread(fid, [2, code_size], 'uchar'); if (cnt ~= 2*code_size) error(sprintf('Problem reading Encodes:%d', ftell(fid))); end curCodes = curCodes(1,:) + curCodes(2,:) * shift1; curCodes = curCodes - (shift2 * (curCodes >= (shift2 / 2))); curEntry{3} = curCodes; end if (eog_size > 0) [curEOG,cnt] = fread(fid, [2, eog_size], 'uchar'); if (cnt ~= 2*eog_size) error(sprintf('Problem reading EOG codes:%d', ftell(fid))); end curEOG = curEOG(1,:) + curEOG(2,:) * shift1; curEOG = curEOG - (shift2 * (curEOG >= (shift2 / 2))); curEntry{4} = curEOG; end if (epp_size > 0) [curEPP,cnt] = fread(fid, [2, epp_size], 'uchar'); if (cnt ~= 2*epp_size) error(sprintf('Problem reading EPP codes:%d', ftell(fid))); end curEPP = curEPP(1,:) + curEPP(2,:) * shift1; curEPP = curEPP - (shift2 * (curEPP >= (shift2 / 2))); curEntry{5} = curEPP; end list{entry} = curEntry; entry = entry + 1; [header,c] = fread(fid, [26,1], 'uchar'); end fclose(fid);