gets / sets parameters from / to mat file(s) FORMAT mfcells = pr_matfilerw(mfimgs, mfvnames, mfvvals) OR FORMAT mfcells = pr_matfilerw(mfimgs, mfstruct) mfimgs can be filenames of images, or mat files, or vol structs mfvnames is cell array containing names of variables to save mfvvals is cell array with same number of columns as the number of names in mfvnames, containing values of variables, to save with names given in mfvnames. If has more than one row, should have the same number of rows as the number of mat files, with values for each mat file in each row OR mfstruct - (cell array of) structures, fields are variables saved into mat file. If more than one element, should have one element of each mat file, each with a structure to save into the mat file values will be appended to current contents of mat file NB the mat file will be v5 compatible only if modified here mfcells is cell array size of mfimgs, with mat file values in structures Matthew Brett 20/10/00 $Id: pr_matfilerw.m,v 1.1 2005/06/01 09:26:53 matthewbrett Exp $
0001 function mfcells = pr_matfilerw(mfimgs, varargin) 0002 % gets / sets parameters from / to mat file(s) 0003 % FORMAT mfcells = pr_matfilerw(mfimgs, mfvnames, mfvvals) OR 0004 % FORMAT mfcells = pr_matfilerw(mfimgs, mfstruct) 0005 % 0006 % mfimgs can be filenames of images, or mat files, or vol structs 0007 % mfvnames is cell array containing names of variables to save 0008 % mfvvals is cell array with same number of columns as the number of 0009 % names in mfvnames, containing values of variables, to save with names 0010 % given in mfvnames. If has more than one row, should have the same 0011 % number of rows as the number of mat files, with values for each mat 0012 % file in each row 0013 % OR 0014 % mfstruct - (cell array of) structures, fields are variables saved into 0015 % mat file. If more than one element, should have one element of each mat 0016 % file, each with a structure to save into the mat file 0017 % 0018 % values will be appended to current contents of mat file 0019 % NB the mat file will be v5 compatible only if modified here 0020 % 0021 % mfcells is cell array size of mfimgs, with mat file values in structures 0022 % 0023 % Matthew Brett 20/10/00 0024 % 0025 % $Id: pr_matfilerw.m,v 1.1 2005/06/01 09:26:53 matthewbrett Exp $ 0026 0027 if nargin < 1 0028 error('Need image / mat file name(s), +/- params to add') 0029 end 0030 if isstruct(mfimgs) % vol struct to filename 0031 mfimgs = strvcat(mfimgs(:).fname); 0032 end 0033 mf.nimgs = size(mfimgs, 1); 0034 0035 mfstruct = {}; 0036 if nargin == 2 % mfstruct form 0037 mfstruct = varargin{1}; 0038 if ~iscell(mfstruct), mfstruct={mfstruct};end 0039 end 0040 if nargin > 2 % mfvnames, mfvvals form 0041 [mfvnames mfvvals] = deal(varargin{1:2}); 0042 if ~iscell(mfvnames), mfvnames = cellstr(mfvnames); end 0043 if ~iscell(mfvvals), mfvvals = {mfvvals};end 0044 mfvnames = mfvnames(:); 0045 nvar = length(mfvnames); 0046 tmp = [size(mfvvals) == nvar]; 0047 if sum(tmp)==0 0048 error('Value arrays must have same no of rows or columns as names'); 0049 end 0050 if all(tmp == [1 0]), mfvvals=mfvvals';end 0051 nstructs = size(mfvvals,1); 0052 mfstruct = cell(nstructs,1); 0053 for r = 1:size(mfvvals,1) 0054 for c = 1:nvar 0055 mfstruct{r} = setfield(mfstruct{r},mfvnames{c},mfvvals{r,c}); 0056 end 0057 end 0058 end 0059 if isempty(mfstruct) 0060 nilnewf = 1; 0061 mfstruct = cell(mf.nimgs,1); 0062 else 0063 nilnewf = 0; 0064 mfstruct = mfstruct(:); 0065 if length(mfstruct)==1 0066 mfstruct = repmat(mfstruct,mf.nimgs,1); 0067 elseif length(mfstruct)~=mf.nimgs 0068 error('Fewer passed values than mat file names'); 0069 end 0070 end 0071 0072 mfcells = cell(mf.nimgs, 1); 0073 for mfi = 1:mf.nimgs; 0074 mf.name = [spm_str_manip(mfimgs(mfi,:), 's') '.mat']; 0075 mf.ef = exist(mf.name, 'file'); 0076 mfstructi = mfstruct{mfi}; 0077 if mf.ef 0078 matvars = load(mf.name); 0079 mfstructi = mars_struct('fillafromb', mfstructi, matvars); 0080 end 0081 if ~nilnewf 0082 savestruct(mf.name, mfstructi); 0083 end 0084 mfcells{mfi} = mfstructi; 0085 end