function ltpdv_fcn_run(varargin)
  % LTPDV_FCN_RUN runs the command on the selected panel
  %
  % M Hewitson 23-04-08
  %
  % $Id: ltpdv_fcn_run.m,v 1.4 2008/09/08 12:31:43 hewitson Exp $
  %

  % Get handles
  myh     = varargin{1};
  tag     = varargin{end-2};
  panH    = varargin{end-1};
  mainfig = varargin{end};

  % Get function
  switch tag
    case 'preproc'
      listtag = 'LTPDVpreprocfcnlist';
      ptag    = 'LTPDVpreprocparampan';
      fcns = ltpdv_preproc_function_list;
    case 'spectral'
      listtag = 'LTPDVspectralfcnlist';
      ptag    = 'LTPDVspectralparampan';
      fcns = ltpdv_spectral_function_list;
    otherwise
      error('### Unknown function set');
  end
  fcnh = findobj(panH, 'Tag', listtag);
  val  = get(fcnh, 'Value');
  strs = get(fcnh, 'String');
  fcn  = strs{val};

  idx = strcmp(fcn, fcns);
  idx = find(idx(:,2));
  fcn = fcns{idx,1};
  
  % Get params
  parampanh = findobj(panH, 'Tag', ptag);
  pl = ltpdv_get_params_from_panel(parampanh);

  % Get selected objects
  objs = ltpdv_get_selected_objs(mainfig);

  % Collect AOs
  as = [];
  for j=1:numel(objs)
    obj = objs{j};
    if isa(obj, 'ao')
      as = [as obj];
    else
      warning('!!! Skipping object %s - it''s not an AO', obj.name);
    end
  end

  % Evaluate fcn
  res = feval(fcn, as, pl);

  % Add result to object list
  for j=1:numel(res)
    objs = getappdata(mainfig, 'LTPDAobjects');
    setappdata(mainfig, 'LTPDAobjects', [objs {res(j)}]);
  end
  % Refresh object list
  ltpdv_refresh_object_list(mainfig);

end

  % END