function [str,skiplst,lowerlst,UPPERlst,words]=...
  camelcase(str,iscomp,FirstCap,islower,skiplst,lowerlst,UPPERlst)
% camelcase(titlecase) Capitalize the first character of each word.
% Usage: str=camelcase(S,iscomp,skip);
%
% Capitalize the first character in every word except those in
% skip=[{'a','an','and','in','for','or','to','the'}]. Use iscomp=[false] to
% compress (ie remove spaces between words) as well.

% revision history:
% 05/22/94 Mark D. Shattuck <mds> camelcase.m
%          Compbine words into camelcase.
% 08/23/95 mds add titlecase w/ skiplst, compress optional iscomp
% 03/19/00 mds Force first word captital, default iscomp=false;
% 01/28/16 mds add lowerlst, upperlst. firstWordCapOptional(FirstCap)
%          islower=force lower. Should really use regexprep

if(~exist('iscomp','var') || isempty(iscomp))
  iscomp=false;
end
if(~exist('islower','var') || isempty(islower))
  islower=true;
end
if(~exist('FirstCap','var') || isempty(FirstCap))
  FirstCap=true;
end
if(exist('skiplst','var') && isempty(skiplst))
else
  if(~exist('skiplst','var') || strcmpi(skiplst(1),'-'))
    skiplst={'a','an','the','at','by','for','in','of','on',...
      'to','up','and','as','but','or','nor','with','is'};
  end
end
if(exist('lowerlst','var') && isempty(lowerlst))
else
  if(~exist('lowerlst','var') || strcmpi(lowerlst(1),'-'))
    lowerlst=skiplst;
  end
end
if(exist('UPPERlst','var') && isempty(UPPERlst))
else
  if(~exist('UPPERlst','var') || strcmpi(UPPERlst(1),'-'))
    UPPERlst={'MRI','NMR','AIP'};
  end
end
skiplst=lower(skiplst);

words=regexp(str,'[ \t]','split');

% camel/title
sk=[0 ismember(lower(words(2:end)),skiplst)]; % first word always capital
if(islower)
  words(~sk)=cellfun(@(x) [upper(x(1)) lower(x(2:end))],words(~sk),'uniformoutput',false);
else
  words(~sk)=cellfun(@(x) [upper(x(1)) x(2:end)],words(~sk),'uniformoutput',false);
end
%force lower
sk=ismember(lower(words),lower(lowerlst));
words(sk)=cellfun(@(x) lower(x),words(sk),'uniformoutput',false);

%force upper
sk=ismember(upper(words),upper(UPPERlst));
words(sk)=cellfun(@(x) upper(x),words(sk),'uniformoutput',false);

if(~FirstCap)
  words{1}(1)=lower(words{1}(1));
else
  words{1}(1)=upper(words{1}(1));
end
if(iscomp)
  str=sprintf('%s',words{:});
else
  str=sprintf('%s ',words{:});
  str=str(1:end-1);
end
end

