% returns a port number, given the port's name or number
%
% n = getPortNum(opt, name, nameList)
%
% Example:
% nPR = getSerialNum(opt, 'PR');
% n = getPortNum(opt, 'fr', opt.optic(nPR).inNames)

function n = getPortNum(opt, name, nameList)

  Nout = length(nameList);

  % if name is really a string, try to match it
  if ischar(name)
    m = [];
    for n = 1:Nout
      m = strmatch(name, nameList{n}, 'exact');
      if ~isempty(m)
        break
      end
    end
    
    if isempty(m)
      error('Invalid output name "%s" used.', name)
    end
  elseif isnumeric(name)
    n = name;
    
    % check serial number range
    if n < 1
      error('output number out of range (%d < 1)', n)
    end
    
    if n > Nout
      error('output number out of range (%d > %d)', n, Nout)
    end
  else
    error('Invalid argument of type %s', class(name));
  end
