function ltpdv_search_channels(varargin)

% LTPDV_SEARCH_CHANNELS search the channel list and reduce it to those that
% match the search criteria.
% 
% M Hewitson 26-07-06
% 
% $Id: ltpdv_search_channels.m,v 1.1 2008/05/11 10:38:43 hewitson Exp $

% Handles
myh = varargin{1};
mainfig = varargin{end};

% channels
ch = findobj(mainfig, 'Tag', 'LTPDVchannelList');
chans = get(ch, 'String');

nchans = size(chans,1);
chansout = [];

% First tokenize the search string
sh = findobj(mainfig, 'Tag', 'LTPDVchannelSearchEdit');
sstr = get(sh, 'String');
curr = upper(sstr);
if isempty(curr)
   [latest, chans] = ltpdv_server_get_latest_chans(mainfig);
   % Set channel list
   ltpdv_server_set_channel_list(mainfig, chans);
else
  
  [t, r] = strtok(curr);
  cstr = [];
  cstr = [cstr; cellstr(t)];
  while ~isempty(r)
    [t, r] = strtok(r);
    cstr = [cstr; cellstr(t)];
  end

  if strcmp(curr, '')
    chansout = chans;
  else
    for str=1:length(cstr)
      for c=1:nchans
        idx = strfind(upper(chans(c,:)), char(cstr(str)));
        if ~isempty(idx)
          chansout = strvcat(chansout, deblank(chans(c,:)));
        end
      end
    end
  end

  if ~isempty(chansout)
    chansout = char(sort(cellstr(chansout)));
  end

  ltpdv_server_set_channel_list(mainfig, chansout);

end


% END