function publishfig(filename,varargin)
% Save the current figure for journal publication
% Usage:   publishfig(filename,<num>)
%          filename is the name used for .tif,.eps and .fig output files
%
% Generates 3 outputs:
%     300 dpi tiff
%     Color encapsulated postscript
%     A Matlab fig file (which can be loaded again to re-save)
%
% Optional arguments that can override defaults are print options:
%     '-deps' for black and white or greyscale postscript
%     '-r150' (e.g.) to change resolution of tiff
% or
%     number (integer) for the figure to print/save (instead of gcf)

% defaults: current figure, colour postscript and 300dpi tiff
h = gcf;

set(h,'inverthardcopy','on')

% set the default options - allows for 
[psopt,resopt,figopt] = publishfigopts;
% psopt = '-depsc';
% resopt = '-r300';
% figopt = 1;

% extra inputs can override defaults
for k=1:length(varargin)
  if isstr(varargin{k})
    str = varargin{k};
    if ~isempty(findstr('ps',str))
      % then option 
      psopt = str;
    else
      resopt = str;
    end
  else
    h = varargin{k};
  end
end

if ~isempty(resopt)
  print(h,'-dtiff',resopt,filename)
end

if ~isempty(findstr('eps',psopt))
  print(h,psopt,'-tiff',filename)
else
  print(h,psopt,filename)
end

if figopt
  hgsave(h,filename)
end

