Home > analyzePRF > utilities > consolidatemat.m

consolidatemat

PURPOSE ^

function results = consolidatemat(files,outfile,varsexclude)

SYNOPSIS ^

function results = consolidatemat(files,outfile,varsexclude)

DESCRIPTION ^

 function results = consolidatemat(files,outfile,varsexclude)

 <files> is a wildcard matching one or more .mat files
 <outfile> (optional) is a .mat file to write 'results' to.
   if [] or not supplied, don't write to a .mat file.
 <varsexclude> (optional) is a variable name or a cell vector of
   variable names to NOT load.  if [] or not supplied, load everything.

 we use matchfiles.m to match the <files>.
 we then construct a struct array with elements 
   containing the results of loading each .mat file.
 this array is named 'results' and we save it
   to <outfile> if supplied.

 example:
 a = 1; b = 2; save('test001.mat','a','b');
 a = 3; b = 4; save('test002.mat','a','b');
 consolidatemat('test*.mat','final.mat');
 results = loadmulti('final.mat','results');
 results(1)
 results(2)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function results = consolidatemat(files,outfile,varsexclude)
0002 
0003 % function results = consolidatemat(files,outfile,varsexclude)
0004 %
0005 % <files> is a wildcard matching one or more .mat files
0006 % <outfile> (optional) is a .mat file to write 'results' to.
0007 %   if [] or not supplied, don't write to a .mat file.
0008 % <varsexclude> (optional) is a variable name or a cell vector of
0009 %   variable names to NOT load.  if [] or not supplied, load everything.
0010 %
0011 % we use matchfiles.m to match the <files>.
0012 % we then construct a struct array with elements
0013 %   containing the results of loading each .mat file.
0014 % this array is named 'results' and we save it
0015 %   to <outfile> if supplied.
0016 %
0017 % example:
0018 % a = 1; b = 2; save('test001.mat','a','b');
0019 % a = 3; b = 4; save('test002.mat','a','b');
0020 % consolidatemat('test*.mat','final.mat');
0021 % results = loadmulti('final.mat','results');
0022 % results(1)
0023 % results(2)
0024 
0025 % TODO: what about mismatches in the contents of the files?
0026 %       save only the intersection?  report to screen?
0027 
0028 % input
0029 if ~exist('outfile','var') || isempty(outfile)
0030   outfile = [];
0031 end
0032 if ~exist('varsexclude','var') || isempty(varsexclude)
0033   varsexclude = [];
0034 end
0035 
0036 % do it
0037 files = matchfiles(files);
0038 clear results;
0039 fprintf('consolidatemat: ');
0040 for p=1:length(files)
0041   statusdots(p,length(files));
0042   if isempty(varsexclude)
0043     a = load(files{p});
0044   else
0045     a = loadexcept(files{p},varsexclude,1);
0046   end
0047   if exist('results','var')
0048     assert(isequal(sort(fieldnames(results(1))),sort(fieldnames(a))), ...
0049            sprintf('unexpected fields in file "%s"',files{p}));
0050   end
0051   results(p) = a;
0052 end
0053 if ~isempty(outfile)
0054   fprintf('saving...');
0055   save(outfile,'results');
0056 end
0057 fprintf('done.\n');

Generated on Wed 18-Jun-2014 21:47:41 by m2html © 2005