function  x = params2vector(params,template)

% PARAMS2VECTOR -- Converts PARAMS structure into a vector.
%   
%   X = PARAMS2VECTOR(PARAMS,TEMPLATE)
%
%   It is convenient to pack model parameters into "params" structures.
%   The function minimization routines such as FMINCON, however, require
%   vector input.  PARAMS2VECTOR and VECTOR2PARAMS convert between the
%   two formats, guided by user-supplied templates.
%
%   PARAMS must be a parameter structure specific for your model.
%   TEMPLATE must be a cell array of strings specifying the conversion.
%   The strings are evaluated (using EVAL) ione after the other and the 
%   resulting values are assigned to successive elts of the output vector.
%   Each string must have the form  'VAL = .... PARAMS .... ;'
%   That is, the first three letters are capital 'VAL', followed by an
%   assignment operator, followed by some expression involving the
%   structure PARAMS (in capital letters), and ending with a semicolon.
%
%   Example:  Consider the template
%     template = {'VAL = PARAMS.alpha ;' , 'VAL = PARAMS.beta ;'}
%   Then the call  x = params2vector(params,template)  will produce a
%   two-element vector storing the fields 'alpha' and 'beta',
%   respectively. The other fields of the PARAMS structure are not
%   reflected in the output vector.
%
%   Example:
%     params = default_params('your_model') ;
%     p2v_templ = sample_template('params2vector') ;
%     x0 = params2vector(params,p2v_templ) ;
%     optimal_x = fmincon('logL',x0,...) ;          % see LOGL
%     v2p_templ = sample_template('vector2params') ;
%     optimal_params = vector2params(optimal_x,params,v2p_templ) ;
%
%   See also VECTOR2PARAMS, SAMPLE_TEMPLATE, LOGL.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2000/06/07 17:30 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

x = zeros(size(template)) ;    % X has the same shape as TEMPLATE

VAL = NaN ;
PARAMS = params ;

for k = 1:length(template)
  eval(template{k}) ;          % update the value in VAL
  x(k) = VAL ;
end

%% Return X
%%%%% End of file PARAMS2VECTOR.M
