function setfontsize(fsize, shiftud)
%
% set_fontsize(fsize)
%
% INPUT ARGUMENTS
%
%  fsize     font size
%  shiftud   shifting the title up or down (pos+shiftud)
%
% OUTPUT ARGUMENTS
%
%  none
%
% PURPOSE
%
%  Changes all text font sizes in the current figure to desired.

%
% Copyright (C) by Esa Alhoniemi
% email esa.alhoniemi@hut.fi
%
% Last modified: 071101
%
% Modified 080705 by Jukka Parviainen
% email parvi@cis.hut.fi
%  - adding: title -> vertical alignment "top" -> "middle"
%  - adding: shiftud

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h = get(gcf, 'children');   % get all axes of the current figure

Inds = [];
for i = 1:length(h)
  if ~strcmp(get(h(i), 'type'), 'axes')
    Inds = [Inds i];
  end
end

h(Inds) = [];

for i = 1:length(h)

  set(h(i), 'fontsize', fsize);  % process axes

  % process title and axis names

  t = get(h(i), 'title');  
% OPTION 1:
%    set(t, 'fontsize', fsize, 'VerticalAlignment', 'middle');
% OPTION 2:
    pos  = get(t, 'Position');
    ylim = get(gca,'YLim');
    if (nargin==2)
      set(t, 'fontsize', fsize, 'VerticalAlignment', 'bottom', 'Position', [pos(1) ylim(1)+shiftud pos(3)]);
    else
      set(t, 'fontsize', fsize, 'VerticalAlignment', 'bottom', 'Position', [pos(1) ylim(1) pos(3)]);
    end;
  x = get(h(i), 'xlabel'); 
    set(x, 'fontsize', fsize);
  y = get(h(i), 'ylabel'); 
    set(y, 'fontsize', fsize);
  z = get(h(i), 'zlabel'); 
    set(z, 'fontsize', fsize);

  % process all texts

  f = findobj(h(i), 'type', 'text');
  l = length(f);
  for j = 1:length(f), set(f(j), 'fontsize', fsize); end

  % process legends (there is something weird here?)

  f = findobj(h(i), 'tag', 'legend');
  for j = 1:length(f)
    ff = get(f(j), 'children');
    fff = findobj(ff,'type','text');
    set(fff, 'fontsize', fsize);
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

