%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    fromPolesAndZeros
%
% DESCRIPTION: Construct a pzmodel from poles and zeros
%
% CALL:        pzm = fromPolesAndZeros(a, pl)
%
% PARAMETER:   pl   - plist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pzm = fromPolesAndZeros(pzm, pli)

  VERSION = '$Id: fromPolesAndZeros.m,v 1.20 2011/08/15 10:10:33 hewitson Exp $';


  % get pzmodel info
  ii = pzmodel.getInfo('pzmodel', 'From Poles/Zeros');

  % Set the method version string in the minfo object
  ii.setMversion([VERSION '-->' ii.mversion]);

  % Combine input plist with default values
  pl = applyDefaults(ii.plists, pli);

  % Set fields
  pzm.gain = find(pl, 'gain');
  ps       = find(pl, 'poles');
  zs       = find(pl, 'zeros');

  % If the poles/zeros in the plist are no pz-objects then convert them
  if ~isa(ps, 'pz') && ~isempty(ps)
    ps = pz(ps);
  end
  if ~isa(zs, 'pz') && ~isempty(zs)
    zs = pz(zs);
  end

  % Add only valid poles
  poles = [];
  for kk = 1:numel(ps)
    if ~isnan(ps(kk).f)
      poles = [poles ps(kk)];
    end
  end
  pl.pset('poles', poles);

  % Add only valid zeros
  zeros = [];
  for kk = 1:numel(zs)
    if ~isnan(zs(kk).f)
      zeros = [zeros zs(kk)];
    end
  end
  pl.pset('zeros', zeros);  
  
  % Set object properties from input plist
  pzm.setObjectProperties(pl);

  % Add history
  pzm.addHistory(ii, pl, [], []);

end
