function f = loadmulti(m,var,dim,gentle) <m> is a pattern that matches one or more .mat files (see matchfiles.m) <var> is a variable name <dim> (optional) is N means the dimension along which to concatenate 0 means do not concatenate; instead, successively merge in new versions of <var>. there are two cases: if <var> is a regular matrix, NaN elements get overwritten with new content. if <var> is a cell matrix, elements that are [] get overwritten with new content. note that the dimensions of <var> should be the same in each file. however, if they aren't, that's okay --- we automatically expand to the bottom and the right as necessary. default: 0. <gentle> (optional) is 0 means die if <var> is not found in a given file. 1 means continue on (and do not crash) if <var> is not found in a given file. default: 0. get <var> from the file(s) named by <m> and either concatenate different versions together or merge different versions together. we issue a warning if no files are named by <m>. we report progress to the command window if the load takes longer than 10 seconds. example: a = {1 2 []}; save('atest1.mat','a'); a = {[] 5 3}; save('atest2.mat','a'); isequal(loadmulti('atest*mat','a',0),{1 2 3})
0001 function f = loadmulti(m,var,dim,gentle) 0002 0003 % function f = loadmulti(m,var,dim,gentle) 0004 % 0005 % <m> is a pattern that matches one or more .mat files (see matchfiles.m) 0006 % <var> is a variable name 0007 % <dim> (optional) is 0008 % N means the dimension along which to concatenate 0009 % 0 means do not concatenate; instead, successively merge 0010 % in new versions of <var>. there are two cases: 0011 % if <var> is a regular matrix, NaN elements get 0012 % overwritten with new content. if <var> is a cell 0013 % matrix, elements that are [] get overwritten with 0014 % new content. note that the dimensions 0015 % of <var> should be the same in each file. 0016 % however, if they aren't, that's okay --- we 0017 % automatically expand to the bottom and the right 0018 % as necessary. 0019 % default: 0. 0020 % <gentle> (optional) is 0021 % 0 means die if <var> is not found in a given file. 0022 % 1 means continue on (and do not crash) if <var> is 0023 % not found in a given file. 0024 % default: 0. 0025 % 0026 % get <var> from the file(s) named by <m> and either 0027 % concatenate different versions together or merge 0028 % different versions together. 0029 % 0030 % we issue a warning if no files are named by <m>. 0031 % 0032 % we report progress to the command window if the load 0033 % takes longer than 10 seconds. 0034 % 0035 % example: 0036 % a = {1 2 []}; 0037 % save('atest1.mat','a'); 0038 % a = {[] 5 3}; 0039 % save('atest2.mat','a'); 0040 % isequal(loadmulti('atest*mat','a',0),{1 2 3}) 0041 0042 % internal constants 0043 toolong = 10; % seconds 0044 0045 % input 0046 if ~exist('dim','var') || isempty(dim) 0047 dim = 0; 0048 end 0049 if ~exist('gentle','var') || isempty(gentle) 0050 gentle = 0; 0051 end 0052 0053 % transform 0054 m = matchfiles(m); 0055 0056 % check sanity 0057 if length(m)==0 0058 warning('no file matches'); 0059 f = []; 0060 return; 0061 end 0062 0063 % do it 0064 stime = clock; didinit = 0; isfirst = 1; 0065 for p=1:length(m) 0066 0067 % report status 0068 if etime(clock,stime) > toolong 0069 if didinit 0070 statusdots(p,length(m)); 0071 else 0072 didinit = 1; 0073 fprintf('loadmulti'); 0074 end 0075 end 0076 0077 % get values from the file 0078 loaded = load(m{p},var); 0079 varnames = fieldnames(loaded); 0080 if isempty(varnames) 0081 if gentle==0 0082 error(sprintf('loadmulti: <var> was not found in some particular file (%s)',m{p})); 0083 end 0084 else 0085 vals = getfield(loaded,varnames{1}); 0086 0087 % special case 0088 if dim==0 0089 0090 if isfirst 0091 f = vals; 0092 isfirst = 0; 0093 else 0094 [f,vals] = equalizematrixdimensions(f,vals); 0095 if iscell(f) 0096 ix = cellfun(@isempty,f); 0097 else 0098 ix = isnan(f); 0099 end 0100 f(ix) = vals(ix); 0101 end 0102 0103 % usual case 0104 else 0105 if isfirst 0106 f = vals; 0107 isfirst = 0; 0108 else 0109 f = cat(dim,f,vals); 0110 end 0111 end 0112 end 0113 0114 end 0115 0116 % report 0117 if etime(clock,stime) > toolong 0118 fprintf('done.\n'); 0119 end