POLYFIT overloads polyfit() function of MATLAB for Analysis Objects. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DESCRIPTION: POLYFIT overloads polyfit() function of MATLAB for Analysis Objects. CALL: b = polyfit(a, pl) VERSION: $Id: polyfit.m,v 1.5 2007/07/26 10:38:07 ingo Exp $ Parameters: 'N' - degree of polynomial to fit 'coeffs' - (optional) coefficients formed e.g. by [p,s] = polyfit(x,y,N); The following call returns a parameter list object that contains the default parameter values: >> pl = polyfit(ao, 'Params') HISTORY: 05-06-07 M Hewitson Creation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function varargout = polyfit(varargin) 0002 % POLYFIT overloads polyfit() function of MATLAB for Analysis Objects. 0003 % 0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0005 % 0006 % DESCRIPTION: POLYFIT overloads polyfit() function of MATLAB for Analysis 0007 % Objects. 0008 % 0009 % CALL: b = polyfit(a, pl) 0010 % 0011 % VERSION: $Id: polyfit.m,v 1.5 2007/07/26 10:38:07 ingo Exp $ 0012 % 0013 % Parameters: 'N' - degree of polynomial to fit 0014 % 'coeffs' - (optional) coefficients 0015 % formed e.g. by [p,s] = polyfit(x,y,N); 0016 % 0017 % The following call returns a parameter list object that contains the 0018 % default parameter values: 0019 % 0020 % >> pl = polyfit(ao, 'Params') 0021 % 0022 % HISTORY: 05-06-07 M Hewitson 0023 % Creation 0024 % 0025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0026 0027 %% Check if this is a call for parameters 0028 if nargin == 2 0029 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0030 in = char(varargin{2}); 0031 if strcmp(in, 'Params') 0032 varargout{1} = getDefaultPL(); 0033 return 0034 end 0035 end 0036 end 0037 0038 ALGONAME = mfilename; 0039 VERSION = '$Id: polyfit.m,v 1.5 2007/07/26 10:38:07 ingo Exp $'; 0040 0041 0042 % Check if this is a call for parameters 0043 if nargin == 1 0044 in = char(varargin{1}); 0045 if strcmp(in, 'Params') 0046 varargout{1} = getDefaultPL(); 0047 return 0048 end 0049 end 0050 0051 % capture input variable names 0052 invars = {}; 0053 as = []; 0054 ps = []; 0055 for j=1:nargin 0056 invars = [invars cellstr(inputname(j))]; 0057 if isa(varargin{j}, 'ao') 0058 as = [as varargin{j}]; 0059 end 0060 if isa(varargin{j}, 'plist') 0061 ps = [ps varargin{j}]; 0062 end 0063 end 0064 0065 % check plist 0066 if isempty(ps) 0067 pl = getDefaultPL(); 0068 else 0069 pl = combine(ps, getDefaultPL); 0070 end 0071 0072 0073 % Degree of polynomial to fit 0074 N = find(pl, 'N'); 0075 coeffs = find(pl, 'coeffs'); 0076 0077 %----------------------- 0078 % Loop over input AOs 0079 bs = []; 0080 na = length(as); 0081 for j=1:na 0082 0083 a = as(j); 0084 0085 %---------------------------- 0086 % Get data 0087 [x,y] = getAOdata(a); 0088 0089 %---------------------------- 0090 % Fit polynomial 0091 0092 if isempty(coeffs) 0093 %[p,s,mu] = polyfit(x,y,N); 0094 [p,s] = polyfit(x,y,N); 0095 0096 % Add coefficients to plist 0097 pl = append(pl, param('coeffs', p)); 0098 else 0099 p = coeffs; 0100 end 0101 0102 % make new values from these coefficients 0103 y = polyval(p, x); 0104 0105 %---------------------------- 0106 % Make output AO/cdata 0107 d = a.data; 0108 0109 d = set_xy_axis(d, x, y); 0110 0111 % if isa(d, 'tsdata') 0112 % d = set(d, 'x', y); 0113 % elseif isa(d, 'fsdata') 0114 % d = set(d, 'xx', y); 0115 % elseif isa(d, 'cdata') 0116 % d = set(d, 'vals', y); 0117 % elseif isa(d, 'xydata') 0118 % d = set(d, 'y', y); 0119 % else 0120 % error('### Unknown data type.') 0121 % end 0122 0123 d = set(d, 'name', sprintf('polyfit(%s)', d.name)); 0124 0125 h = history(ALGONAME, VERSION, pl, a.hist); 0126 h = set(h, 'invars', invars(j)); 0127 0128 % make output analysis object 0129 b = ao(d, h); 0130 0131 % set name 0132 if isempty(invars{j}) 0133 n = a.name; 0134 else 0135 n = invars{j}; 0136 end 0137 b = set(b, 'name', sprintf('polyfit(%s)', n)); 0138 0139 % add to outputs 0140 bs = [bs b]; 0141 0142 end 0143 0144 %--------------------- 0145 % Set outputs 0146 varargout{1} = bs; 0147 0148 %-------------------------------------------------------------------------- 0149 % Get default params 0150 function plo = getDefaultPL() 0151 0152 disp('* creating default plist...'); 0153 plo = plist(); 0154 plo = append(plo, param('N', 1)); 0155 disp('* done.'); 0156 0157 % END 0158