function [ c ] = pnLowestValue(data,featStr)
%	[ c ] = pnLowestValue(data) calculates the first measurement recorded.
%	For each feature, the above names are used as suffixes in the output
%	feature label, i.e. for Albumin, the function would output
%	AlbuminLowest, etc.
%   
%	[ c ] = pnLowestValue(data, featStr) allows specification of a subset
%	of features to extract granularity values for. featStr should be a
%	string if features are extracted from a single field, or a cell array 
%	of strings for feature extraction from multiple fields.
%
%	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
%		
%       featStr - String of single field to extract features from OR cell
%           array of strings of multiple fields to extract features from.
%       
%	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);
%		c = pnLowestValue(data(1,:)) %	
%	See also PNGENERATEFEATURES

%	References:
%		Physionet Challenge 2012

%	Copyright 2012 Alistair Johnson

%	$LastChangedBy: alistair $
%	$LastChangedDate: 2012-06-04 15:40:13 -0400 (Mon, 04 Jun 2012) $
%	$Revision: 28 $
%	Originally written on GLNXA64 by Alistair Johnson, 11-May-2012 09:12:20
%	Contact: alistairewj@gmail.com

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

genFeatStr = 'Lowest';

%=== Lowest, get index of elements with recordings
idxExist = cellfun(@(x) ~isempty(x), data(:,2));
[fieldMin,idxMin] = cellfun(@min, data(idxExist,4),'UniformOutput',false);
Nexist = sum(idxExist);
%=== Feature labels
featStrCell = {strcat(featStr,genFeatStr)};
c(idxExist,3) = cellfun(@(x) [x;featStrCell],...
    c(idxExist,3), 'UniformOutput',false);

%=== Time stamps
c(idxExist,2) = cellfun(@(x,y,z) [x;y(z)],...
    c(idxExist,2), data(idxExist,2), idxMin, 'UniformOutput',false);

%=== Calculate features, min/max/avg/std, output to cell in ONE LINE!
c(idxExist,4) = cellfun(@(x,y) [x;y],...
    c(idxExist,4), fieldMin, 'UniformOutput', false);

N_notexist = N - Nexist;
c(~idxExist,4) = repmat({NaN},N_notexist,1);
c(~idxExist,2) = repmat({0},N_notexist,1);
c(~idxExist,3) = repmat({featStrCell},N_notexist,1);
end