function [ c ] = pnExtractFeatures(data,featFcn,featStr)
%PNEXTRACTFEATURES	Extract selected features from given fields
%	[ c ] = pnExtractFeatures(data,featFcn,featStr) extracts features using
%	the given feature generation function from the variables specified in
%	featStr. If featStr is left empty, the default (original) variables are
%	used.
%	
%	Inputs:
%		data	- Cell array of data.
%				Column 1 - Subject IDs
%				Column 2 - Time stamp vectors for each subject
%				Column 3 - Feature name vectors for each subject
%				Column 4 - Data value vectors for each subject
%		
%
%	Outputs:
%		c	- A cell of the same size as data in standard format (above)
%		There exist multiple feature labels in Column 3.
%		
%
%	Example
%		bpath = './set-a/';
%		data = pnLoadTextFilesCell(bpath);
%		stat = pnExtractFeatures(data(1,:)) %	
%	See also PNGENERATEFEATURES

%	References:
%		Physionet Challenge 2012

%	Copyright 2012 Alistair Johnson

%	$LastChangedBy: alistair $
%	$LastChangedDate: 2012-06-06 06:00:26 -0400 (Wed, 06 Jun 2012) $
%	$Revision: 32 $
%	Originally written on GLNXA64 by Alistair Johnson, 04-Jun-2012 18:19:06
%	Contact: alistairewj@gmail.com

if nargin<1
    c = [];
    return;
end
if nargin<2
    c = data;
    return;
elseif ischar(featFcn)
    featFcn = {featFcn};
end
if nargin<3 %=== Default: extract info from all features
    featStr = {'Albumin','ALP','ALT','AST','Bilirubin',...
        'BUN','Cholesterol','Creatinine','DiasABP','FiO2',...
        'GCS','Glucose','HCO3','HCT','HR',...
        'K','Lactate','Mg','MAP','MechVent',...
        'Na','NIDiasABP','NIMAP','NISysABP','PaCO2',...
        'PaO2','pH','Platelets','RespRate','SaO2',...
        'SysABP','Temp','TroponinI','TroponinT','Urine','WBC','Weight'};
elseif ischar(featStr)
    featStr = {featStr}; % encapsulate in cell array of strings
end

N = size(data,1);
c = cell(N,4);
c(:,1) = data(:,1);

for f=1:numel(featStr)
    %=== Extract data for a single field
    [ field ] = pnExtractField(data, featStr{f});
    for e=1:numel(featFcn) %=== Extract features for that field
        [ tmpData ] = feval(featFcn{e},field,featStr{f});
        c = pnImputeField(c,tmpData);
    end
end

end
