function logData(datafile, trial, varargin) %-------------------------------------------------------------------------- % USAGE: logData(datafile, trial, [recordedVariables],...) % % Creates a Data struct with each field representing a variable that has % been specified to be saved. % % datafile: Path and name of subject's datafile. Should be a .mat file, % containing a Data structure for that subject. % % trial: Trial number for recording. If variable is a single instance (e.g. % group membership, date run, etc., trial = 1 % % recordedVariables: % % USAGE 1: logData(datafile, [trial], var1, var2, var3) % % This usage will write the variables var1 etc. into the Data structure, % using the names 'var1', etc. % % USAGE 2: logData(datafile, [trial], Struct) % % This usage is a more compact way of logging the data. Struct contains all % the variables for that trial, each with its own field (e.g. Struct.var1, % Struct.var2 etc.). Each field in Struct will be appended to the Data % struct. %-------------------------------------------------------------------------- % Load the datafile where the information is to be saved. If the requested % datafile doesn't exist, create it, but give a warning that one is doing so. if exist(datafile,'file') load(datafile); % loads the Data structure into memory for appending else warning(['Requested datafile (', datafile, ') does not exist!', ... 'The file will be created in the current directory.']); end % Loop over the number of inputs (minus the datafilename and the trial % number), and for each input, determine whether it is a single value (in % which case revert to usage #1) or a structure (in which case revert to % usage #2). for var = 1:nargin - 2 if ~isstruct(varargin{var}) % USAGE 1: use the inputname function to retrieve the name value of % each variable submitted to the function, then store this variable % in the corresponding field of the Data structure. Data.(inputname(var + 2)){trial} = varargin{var}; else % USAGE 2: given a structure with field names var1, var2, etc., % save the data in each field to the appropriate location in the % corresponding field of the Data structure. TempStruct = varargin{var}; varnames = fieldnames(TempStruct); for field = 1:length(varnames) Data.(varnames{field}){trial} = TempStruct.(varnames{field}); end end end % Having finished appending the data into the Data structure, save it save(datafile,'Data');