


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.10 2007/11/26 14:48:13 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.10 2007/11/26 14:48:13 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 ALGONAME = mfilename; 0028 VERSION = '$Id: polyfit.m,v 1.10 2007/11/26 14:48:13 ingo Exp $'; 0029 bs = []; 0030 0031 %% Check if this is a call for parameters 0032 if nargin == 2 0033 if isa(varargin{1}, 'ao') && ischar(varargin{2}) 0034 in = char(varargin{2}); 0035 if strcmp(in, 'Params') 0036 varargout{1} = getDefaultPL(); 0037 return 0038 elseif strcmp(in, 'Version') 0039 varargout{1} = VERSION; 0040 return 0041 end 0042 end 0043 end 0044 0045 %% Collect input ao's, plist's and ao variable names 0046 in_names = {}; 0047 for ii = 1:nargin 0048 in_names{end+1} = inputname(ii); 0049 end 0050 0051 [as, ps, invars] = collect_inputs(varargin, in_names); 0052 0053 % check plist 0054 if isempty(ps) 0055 pl = getDefaultPL(); 0056 else 0057 pl = combine(ps, getDefaultPL); 0058 end 0059 0060 0061 % Degree of polynomial to fit 0062 N = find(pl, 'N'); 0063 coeffs = find(pl, 'coeffs'); 0064 0065 %----------------------- 0066 % Loop over input AOs 0067 for j=1:numel(as) 0068 0069 a = as(j); 0070 0071 %---------------------------- 0072 % Get data 0073 [x,y] = get_xy_values(a.data); 0074 0075 %---------------------------- 0076 % Fit polynomial 0077 0078 if isempty(coeffs) 0079 %[p,s,mu] = polyfit(x,y,N); 0080 [p,s] = polyfit(x,y,N); 0081 0082 % Add coefficients to plist 0083 pl = pset(pl, 'coeffs', p); 0084 else 0085 p = coeffs; 0086 end 0087 0088 % make new values from these coefficients 0089 y = polyval(p, x); 0090 0091 %---------------------------- 0092 % Make output AO/cdata 0093 d = a.data; 0094 d = set_xy_axis(d, x, y); 0095 d = set(d, 'name', sprintf('polyfit(%s)', d.name)); 0096 0097 h = history(ALGONAME, VERSION, pl, a.hist); 0098 h = set(h, 'invars', invars(j)); 0099 0100 % make output analysis object 0101 b = ao(d, h); 0102 0103 % set name 0104 b = setnh(b, 'name', sprintf('polyfit(%s)', invars{j})); 0105 0106 % add to outputs 0107 bs = [bs b]; 0108 0109 end 0110 0111 % Reshape the ouput to the same size of the input 0112 bs = reshape(bs, size(as)); 0113 0114 %--------------------- 0115 % Set outputs 0116 varargout{1} = bs; 0117 0118 %-------------------------------------------------------------------------- 0119 % Get default params 0120 function plo = getDefaultPL() 0121 0122 plo = plist(); 0123 plo = append(plo, param('N', 1)); 0124 plo = append(plo, param('coeffs', [])); 0125 0126 % END 0127