Home > cellorganizer > utilities > ml_initparam.m

ml_initparam

PURPOSE ^

TZ_INITPARAM Initialize parameters.

SYNOPSIS ^

function param = ml_initparam(param,paramdefault)

DESCRIPTION ^

TZ_INITPARAM Initialize parameters.
   PARAM2 = TZ_INITPARAM(PARAM,PARAMDEFAULT) returns the initialized 
   parameter. PARAM and PARAMDEFAULT are both structures. All fields
   in PARAM will be kept unchanged in PARAM2. And all fields in
   PARAMDEFAULT but not in PARAM will be added to PARAM2. If a field is a
   structure, parameter initialization will go down herachically.
   
   Example:
       param2 =  ml_initparam(struct('t',1,'t2',2),struct('t2',3,'t3',4))
       returns
           param2 = 
               t: 1
               t2: 2
               t3: 4
     
       param2 =  ml_initparam(struct('t',1,'t2',struct('t3',2)), ...
               struct('t1',3,'t2',struct('t4',4)))
       returns
           param2 = 
               t: 1
               t2: struct('t3',2,'t4',4)
               t1: 3

   See also

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function param = ml_initparam(param,paramdefault)
0002 %TZ_INITPARAM Initialize parameters.
0003 %   PARAM2 = TZ_INITPARAM(PARAM,PARAMDEFAULT) returns the initialized
0004 %   parameter. PARAM and PARAMDEFAULT are both structures. All fields
0005 %   in PARAM will be kept unchanged in PARAM2. And all fields in
0006 %   PARAMDEFAULT but not in PARAM will be added to PARAM2. If a field is a
0007 %   structure, parameter initialization will go down herachically.
0008 %
0009 %   Example:
0010 %       param2 =  ml_initparam(struct('t',1,'t2',2),struct('t2',3,'t3',4))
0011 %       returns
0012 %           param2 =
0013 %               t: 1
0014 %               t2: 2
0015 %               t3: 4
0016 %
0017 %       param2 =  ml_initparam(struct('t',1,'t2',struct('t3',2)), ...
0018 %               struct('t1',3,'t2',struct('t4',4)))
0019 %       returns
0020 %           param2 =
0021 %               t: 1
0022 %               t2: struct('t3',2,'t4',4)
0023 %               t1: 3
0024 %
0025 %   See also
0026 
0027 % Copyright (C) 2006  Murphy Lab
0028 % Carnegie Mellon University
0029 %
0030 % This program is free software; you can redistribute it and/or modify
0031 % it under the terms of the GNU General Public License as published
0032 % by the Free Software Foundation; either version 2 of the License,
0033 % or (at your option) any later version.
0034 %
0035 % This program is distributed in the hope that it will be useful, but
0036 % WITHOUT ANY WARRANTY; without even the implied warranty of
0037 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0038 % General Public License for more details.
0039 %
0040 % You should have received a copy of the GNU General Public License
0041 % along with this program; if not, write to the Free Software
0042 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0043 % 02110-1301, USA.
0044 %
0045 % For additional information visit http://murphylab.web.cmu.edu or
0046 % send email to murphy@cmu.edu
0047 
0048 %   18-Nov-2005 Initial write T. Zhao
0049 %   Copyright (c) Center for Bioimage Informatics, CMU
0050 
0051 if nargin < 2
0052     error('Exactly 2 arguments are required')
0053 end
0054 
0055 if isempty(param)
0056     param = paramdefault;
0057     return;
0058 end
0059 
0060 defaultParameterNames = fieldnames(paramdefault);
0061 
0062 for k=1:length(defaultParameterNames)
0063     defaultParameterValue = ...
0064         getfield(paramdefault,defaultParameterNames{k});
0065     if ~isfield(param,defaultParameterNames{k})
0066         param = setfield(param,defaultParameterNames{k}, ...
0067             defaultParameterValue);
0068     else
0069         if isstruct(defaultParameterValue)
0070             parameterValue = getfield(param,defaultParameterNames{k});
0071             param = setfield(param,defaultParameterNames{k}, ...
0072                 ml_initparam(parameterValue,defaultParameterValue));
0073         end
0074     end
0075 end

Generated on Sun 29-Sep-2013 18:44:06 by m2html © 2005