% loadrlr.m % % This Matlab 5.3/R11 script loads *.rlr raw data files written by % Ratlab 2000. % Ratlab 2000 is the datataking program for the Caltech class Bi 162, % written in LabView. % % This script clobbers the following workspace variables: % datapath, workpath, filename, fid, message, line, paramname, % done, vals, nextval, filepos, rawlen % Of these, it leaves datapath, workpath, and filename in the workspace, % but clears the rest before exiting % % This script also sets various other parameters and data variables % in the workspace, which is the whole point. % See the code for details. ;-) % % David Kewley % May 17, 2000 % % Modified by Matt Cook and Dan Song the next day, % changing 'single' to 'float32' and changing 'str2double' to 'str2num' % so that it would run on our version of Matlab, 5.2.0.3084. % workpath = pwd; if exist ('datapath') cd (datapath); end [filename, datapath] = uigetfile ('*.rlr', 'Select file to load:'); [fid, message] = fopen ([datapath filename], 'r'); if fid == -1 disp (message); else % all the file-reading and -parsing work is done now line = fgetl (fid); if ~isstr (line) disp ('Could not read file header.'); else if line ~= 'RATLAB' disp ('File does not appear to be a Ratlab file.') else % clear all the workspace variables that we might read from the file clear FileVersion DateTime TrialRecTime TrialInterval RecRate clear NumRecChans NumRecSamp PreStimtime StimChanNames clear NumStimChans NumStimSamp StimRate StimData RawData % Loop over parameter lines in the header, after the initial % 'RATLAB'. % Set the parameter workspace variables that we find % in the header. done = 0; while ~done line = fgetl (fid); [paramname, vals] = strtok (line); switch paramname case { 'Version', 'FileVersion' } FileVersion = str2num ( strtok (vals) ); case 'DateTime' DateTime = vals; case 'TrialRecTime' TrialRecTime = str2num ( strtok (vals) ); case 'TrialInterval' TrialInterval = str2num ( strtok (vals) ); case 'RecRate' RecRate = str2num ( strtok (vals) ); case 'NumRecChans' NumRecChans = str2num ( strtok (vals) ); case 'NumRecSamp' NumRecSamp = str2num ( strtok (vals) ); case 'PreStimTime' PreStimTime = str2num ( strtok (vals) ); case 'StimChanNames' while (length(vals)) StimChanNames = cell (0); [nextval, vals] = strtok (vals); if strcmp (nextval, 'NoStim') StimChanNames = {StimChanNames{:}, nextval}; end end NumStimChans = length (StimChanNames); case 'NumStimSamp' NumStimSamp = str2num ( strtok (vals) ); case 'StimRate' StimRate = str2num ( strtok (vals) ); case 'EndOfHeader' done = 1; otherwise disp (['Unknown parameter in header: ' paramname]); end end % end of while loop over parameter lines in header % close the file and reopen it as a stream of % big-endian single-precision floats % but remember where we were, and start reading there filepos = ftell (fid); if fclose (fid) disp ('error closing the file'); end [fid, message] = fopen ([datapath filename], 'r', 'ieee-be'); if fid == -1 disp (message); else fseek (fid, filepos, 'bof'); % move back to where we were % now read stim waveforms (if any) if exist ('NumStimSamp') & exist ('NumStimChans') if NumStimSamp > 0 & NumStimChans > 0 % Note: the early lazy version of Ratlab 2000 datasaving % doesn't actually save the stim waveforms, even if % stimulation occurred. In this case, the StimChanNames % will contain the appropriate stim channel names, but % NumStimSamp will be zero. There is no stim data in % the file, so we should not attempt to read any. % read stim waveforms StimData = fread (fid, ... [NumStimChans, NumStimSamp], 'float32'); end end % get the length of the raw data portion of the file % and calculate the number of trials filepos = ftell (fid); fseek (fid, 0, 'eof'); % go to end of file rawlen = ftell (fid) - filepos; fseek (fid, filepos, 'bof'); NumTrials = rawlen / (NumRecChans * NumRecSamp); if NumTrials ~= fix (NumTrials) disp ('Raw data does not fit evenly into trials.'); else % read raw data if NumRecChans == 1 RawData = fread (fid, [NumRecSamp, NumTrials], 'float32'); else RawData = fread (fid, ... [NumRecChan, NumRecSamp, NumTrials], 'float32'); end end end end end if fclose (fid) disp ('error closing the file'); end end % Clean up the temporary variables from the workspace and go back to % the directory we started in. clear done fid line message nextval paramname vals filepos rawlen cd (workpath); % Bye!