function [f,msg] = todffir(dffirstruct,varargin)
%TODFFIR   Convert a DFILT object to a DFFIR object.
%   This function is used for all direct form conversions
%   for fir filters, such as df, dft, dfsym, dfasym.

%   Author(s): R. Losada
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.2 $  $Date: 2000/05/23 18:11:40 $

error(nargchk(2,3,nargin));

msg = '';

% Create a dummy df fir object to check that tfstruct is a valid string
errormsg = 'Invalid structure specified.';
try,
    dummy = feval(dffirstruct); % This creates a default filter of the given structure
    if ~isa(dummy,'fir'),
        msg = errormsg;
        return
    end
catch
    msg = errormsg;
    return
end

h = varargin{1};

% Make h a single-section dfilt if the conversion is wanted for a particular section
if nargin == 3,
    h = section(h,varargin{2});
end

if ~isfir(h),
    msg = sprintf('Can only convert FIR filters to a %s',upper(dffirstruct));
    return
end

for n = 1:nsections(h),
    [num{n},den{n}] = tf(section(h,n));
    newsection{n} = {num{n}};
end

connect = connection(h);

f = feval(dffirstruct,newsection,connect);

