
function [ found, index ] = ispresent( elements, string, varargin )

% Find string within strin, cell array of file.
%
% Inputs:
% string   = string (allows partial match) or cell string array
%            (exact match) to find
% elements = cell array of strings or text file path (add flag 'file')
%
% Example;
% string = 'This is a string';
% or string = {'This','is','a','cell','string','array'};
% elements = {'string', 'cell'};
%
% Last modified April 15, 2015
% Ben Raanan



if nargin < 2
    
    string = [];
    
    % Case elements is file path
elseif nargin > 2 && strcmpi(varargin,'file')
    
    fid = fopen(elements, 'r');
    txt = textscan(fid, '%s', 'Delimiter', '\n');
    elements = txt{1};
    fclose(fid);
    
end

% replace empty cells w/ NaN
elements(cellfun('isempty',elements)) = {'NaN'};

% Case cell string array
if iscell(string)
    
    m = length(string);
    n = length(elements);
    idx = false(m,n);
    
    for c = 1:m
        stf = strfind(elements,string{c});
        idx(c,:) = ~cellfun('isempty',stf);
    end
    index = sum(idx,1)~=0;
    
    % Case string
elseif ischar(string)
    
    stf = strfind(elements,string);
    index = ~cellfun('isempty',stf);
    
elseif isempty(string)
    
    index = 1:numel(elements);
    
end

found = elements(index);

end
