Home > data-utility > collate_variables.m

collate_variables

PURPOSE ^

Copy variables so they're organized by name, then subject: varname/123.mat

SYNOPSIS ^

function collate_variables(destination, subjects, variables)

DESCRIPTION ^

Copy variables so they're organized by name, then subject: varname/123.mat

 USAGE:
   collate_variables(DESTINATION, SUBJECTS)
       Creates a directory structure under the directory DESTINATION, with
       one directory for each variable that exists for any of the
       SUBJECTS.  Each variable
       <subject>/derived/<var_type>_<var_name>.mat is then copied to
       DESTINATION/<var_name>/<var_type>_<subject>.mat

   collate_variables(DESTINATION, SUBJECTS, VARIABLES)
       Same as above, but only copies the variables that you list in
       VARIABLES, a cell array of strings.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function collate_variables(destination, subjects, variables)
0002 %Copy variables so they're organized by name, then subject: varname/123.mat
0003 %
0004 % USAGE:
0005 %   collate_variables(DESTINATION, SUBJECTS)
0006 %       Creates a directory structure under the directory DESTINATION, with
0007 %       one directory for each variable that exists for any of the
0008 %       SUBJECTS.  Each variable
0009 %       <subject>/derived/<var_type>_<var_name>.mat is then copied to
0010 %       DESTINATION/<var_name>/<var_type>_<subject>.mat
0011 %
0012 %   collate_variables(DESTINATION, SUBJECTS, VARIABLES)
0013 %       Same as above, but only copies the variables that you list in
0014 %       VARIABLES, a cell array of strings.
0015 %
0016 
0017 if ~ exist('variables', 'var')
0018     % Collect the names of all the variables we'll need
0019     vars_per_subject = arrayfun(@list_variables, subjects, 'UniformOutput', 0);
0020     all_variables = unique(vertcat(vars_per_subject{:}));
0021 else
0022     if ischar(variables)
0023         variables = {variables};
0024     end
0025     all_variables = variables;
0026 end
0027 
0028 % start copying things!
0029 for V = 1:numel(all_variables)
0030     var_name = all_variables{V};
0031     var_dir = fullfile(destination, var_name);
0032     
0033     if ~exist(var_dir, 'dir')
0034         mkdir(var_dir);
0035     end
0036     
0037     fprintf('Collecting %s', var_name);
0038     for S = 1:numel(subjects)
0039         subject = subjects(S);
0040         
0041         if ~ has_variable(subject, var_name)
0042             continue
0043         end
0044         
0045         data_type = get_data_type(var_name);
0046         dest_file = fullfile(var_dir, ...
0047             sprintf('%s_%d.mat', data_type, subject));
0048         
0049         %copyfile(get_variable_path(subject, var_name), dest_file);
0050         var_data = get_variable(subject, var_name);
0051         sdata.data = var_data;
0052         sdata.variable = sprintf('%s_%d',var_name,subject);
0053         sdata.info.timestamp = datestr(now);
0054         sdata.info.subject = subject;
0055         sdata.info.path = var_dir;
0056 
0057         save(dest_file, 'sdata');
0058         
0059         fprintf('.');
0060     end
0061     
0062     fprintf('\n');
0063 end
0064

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005