function Size = savefig(Dateiname, Handle)
%Size = savefig(FileName, HANDLE);
%         
%       SAVEFIG stores a FIGURE with a specified HANDLE to disk. Later you can restore this 
%       FIGURE by calling the m-file LOADFIG. If no HANDLE is specified the actuell FIGURE is
%       stored. If no error occures the result is the size of the generated file, otherwith its -1.
%       This version is not able to store UICONTROL and UIMENU. 
%       Here is an example:
%     
%       >>h=figure                            %opens a figure and specifies the figures handle h
%       >>imagesc(rand(20,20))                %plots an scaled image
%       >>S=savefig('c:\my_path\my_fig',h)    %saves the specified figure (my_path must exist)
%
%       If no FileName is given, SAVEFIG opens a file select box. Your last selected save path was stored
%       in SAVEFIG.INI. 
%       This m-file is tested with MATLAB Version 4.2c.1.
%       
%       
%       Version 0.41  10.06.96
%       
%       Jśrg Heckenbach           email  heckenbach@fgan.de  
%       FGAN/FHP/RuS              tel    +49 228 9435 367
%       D-53343 Wachtberg         fax    +49 228 856977


if nargin < 2
  Handle=gcf;
end

figure(Handle)

if nargin < 1
  if exist('savefig.ini');
    eval('load savefig.ini -mat;');
  else
    disp('Couldn''t find SAVEFIG.INI');
    Pfad=[pwd, '\'];
  end

  HomePath = [pwd, '\'];
  if exist('Pfad');
    eval(['cd ',Pfad(1,1:length(Pfad)-1),';']);
  end;
  HeadLine = ['Save Figure No.', int2str(Handle)];
  [Dateiname,Pfad]=uiputfile('*.fig',HeadLine);
  eval(['cd ',HomePath(1,1:length(HomePath)-1),';']);
  if Pfad
    eval('save savefig.ini Pfad;');
  end
else
  Pfad=('');
end

if Dateiname
else
  break
end

   
FigProp = ...
       ['BackingStore         ';...
        'Color                ';...
        'Colormap             ';...
        'CurrentAxes          ';...
        'CurrentCharacter     ';...
        'CurrentMenu          ';...
        'CurrentObject        ';...
        'CurrentPoint         ';...
        'FixedColors          ';...
        'InvertHardcopy       ';...
        'KeyPressFcn          ';...
        'MenuBar              ';...
        'MinColormap          ';...
        'Name                 ';...
        'NextPlot             ';...
        'NumberTitle          ';...
        'PaperUnits           ';...
        'PaperOrientation     ';...
        'PaperPosition        ';...
        'PaperSize            ';...
        'PaperType            ';... 
        'Pointer              ';...
        'Position             ';...
        'Resize               ';...
        'SelectionType        ';...
        'ShareColors          ';...
        'Units                ';...
        'WindowButtonDownFcn  ';...
        'WindowButtonMotionFcn';...
        'WindowButtonUpFcn    ';...
        'ButtonDownFcn        ';...
        'Children             ';...
        'Clipping             ';...
        'Interruptible        ';...
        'Parent               ';...
        'Type                 ';...
        'UserData             ';...
        'Visible              '];
        
for k=1:size(FigProp,1);
  eval(['Fig',FigProp(k,:), '=get(Handle,''', FigProp(k,:), ''');']);
end

AxesProp = ...
       ['AspectRatio       ';...
        'Box               ';...
        'CLim              ';...
        'CLimMode          ';...
        'Color             ';...
        'CurrentPoint      ';...
        'ColorOrder        ';...
        'DrawMode          ';...
        'FontAngle         ';...
        'FontName          ';...
        'FontSize          ';...
        'FontStrikeThrough ';...
        'FontUnderline     ';...
        'FontWeight        ';...
        'GridLineStyle     ';...
        'LineStyleOrder    ';...
        'LineWidth         ';...
        'NextPlot          ';...
        'Position          ';...
        'TickLength        ';...
        'TickDir           ';...
        'Title             ';...
        'Units             ';...
        'View              ';...
        'XColor            ';...
        'XDir              ';...
        'Xform             ';...
        'XGrid             ';...
        'XLabel            ';...
        'XLim              ';...
        'XLimMode          ';...
        'XScale            ';...
        'XTick             ';...
        'XTickLabels       ';...
        'XTickLabelMode    ';...
        'XTickMode         ';...
        'YColor            ';...
        'YDir              ';...
        'YGrid             ';...
        'YLabel            ';...
        'YLim              ';...
        'YLimMode          ';...
        'YScale            ';...
        'YTick             ';...
        'YTickLabels       ';...
        'YTickLabelMode    ';...
        'YTickMode         ';...
        'ZColor            ';...
        'ZDir              ';...
        'ZGrid             ';...
        'ZLabel            ';...
        'ZLim              ';...
        'ZLimMode          ';...
        'ZScale            ';...
        'ZTick             ';...
        'ZTickLabels       ';...
        'ZTickLabelMode    ';...
        'ZTickMode         ';...
        'ButtonDownFcn     ';...
        'Children          ';...
        'Clipping          ';...
        'Interruptible     ';...
        'Parent            ';...
        'Type              ';...
        'UserData          ';...
        'Visible           '];
        
AxesHandle = FigChildren;

for l=1:length(AxesHandle)
  for k=1:size(AxesProp,1);
    eval(['A',int2str(l),AxesProp(k,:), '=get(',num2str(AxesHandle(l),16),',''', AxesProp(k,:), ''');']);
  end
end


LineProp = ...
       ['Color        ';...  
        'EraseMode    ';...  
        'LineStyle    ';...  
        'LineWidth    ';...  
        'MarkerSize   ';...  
        'Xdata        ';...  
        'Ydata        ';...  
        'Zdata        ';...  
        'ButtonDownFcn';...  
        'Children     ';...  
        'Clipping     ';...  
        'Interruptible';...  
        'Parent       ';...  
        'UserData     ';...  
        'Visible      '];  

SurfProp = ...                      
       ['CData        ';...
        'EdgeColor    ';...
        'EraseMode    ';...
        'FaceColor    ';...
        'LineStyle    ';...
        'LineWidth    ';...
        'MarkerSize   ';...
        'MeshStyle    ';...
        'Xdata        ';...  
        'Ydata        ';...  
        'Zdata        ';...  
        'ButtonDownFcn';...  
        'Children     ';...  
        'Clipping     ';...  
        'Interruptible';...  
        'Parent       ';...  
        'UserData     ';...  
        'Visible      '];  

ImageProp = ...                      
       ['CData        ';...
        'XData        ';...
        'YData        ';...
        'ButtonDownFcn';...  
        'Children     ';...  
        'Clipping     ';...  
        'Interruptible';...  
        'Parent       ';...  
        'UserData     ';...  
        'Visible      '];  

PatchProp = ...                      
       ['CData        ';...
        'EdgeColor    ';...
        'EraseMode    ';...
        'FaceColor    ';...
        'LineWidth    ';...
        'XData        ';...
        'YData        ';...
        'ZData        ';...
        'ButtonDownFcn';...  
        'Clipping     ';...  
        'Interruptible';...  
        'UserData     ';...  
        'Visible      '];  

TextProp = ...
       ['Color              ';...  
        'EraseMode          ';...  
        'FontAngle          ';...  
        'FontName           ';...  
        'FontSize           ';...  
        'FontWeight         ';...  
        'HorizontalAlignment';...  
        'Position           ';...  
        'Rotation           ';...
        'String             ';...
        'Units              ';...
        'VerticalAlignment  ';...
        'ButtonDownFcn      ';...  
        'Children           ';...  
        'Clipping           ';...  
        'Interruptible      ';...  
        'Parent             ';...  
        'UserData           ';...  
        'Visible            '];  


for m=1:length(AxesHandle)
  eval(['ObjHandle = A',int2str(m),'Children;']);

  for l=1:length(ObjHandle)

    ObjType = get(ObjHandle(l), 'Type');
    eval(['O',int2str(l),'A',int2str(m),'Type = ObjType;']);
  
    if strcmp(ObjType,'line')
      for k=1:size(LineProp,1);
        eval(['O',int2str(l),'A',int2str(m),LineProp(k,:), ...
             '=get(',num2str(ObjHandle(l),16),',''', LineProp(k,:), ''');']);
      end
  
    elseif strcmp(ObjType,'surface')
      for k=1:size(SurfProp,1);
        eval(['O',int2str(l),'A',int2str(m),SurfProp(k,:), ...
             '=get(',num2str(ObjHandle(l),16),',''', SurfProp(k,:), ''');']);
      end
  
    elseif strcmp(ObjType,'image')
      for k=1:size(ImageProp,1);
        eval(['O',int2str(l),'A',int2str(m),ImageProp(k,:), ...
             '=get(',num2str(ObjHandle(l),16),',''', ImageProp(k,:), ''');']);
      end
  
    elseif strcmp(ObjType,'patch')
      for k=1:size(PatchProp,1);
        eval(['O',int2str(l),'A',int2str(m),PatchProp(k,:), ...
             '=get(',num2str(ObjHandle(l),16),',''', PatchProp(k,:), ''');']);
      end
  
      elseif strcmp(ObjType,'text')
      for k=1:size(TextProp,1);
        eval(['O',int2str(l),'A',int2str(m),TextProp(k,:), ...
             '=get(',num2str(ObjHandle(l),16),',''', TextProp(k,:), ''');']);
      end

    end %if strcmp...

  end %for l...


  for k=1:size(TextProp,1);
    eval(['T',int2str(m),TextProp(k,:), '=get(',num2str(eval(['A',int2str(m),'Title']),16),',''', TextProp(k,:), ''');']);
  end
  for k=1:size(TextProp,1);
    eval(['X',int2str(m),TextProp(k,:), '=get(',num2str(eval(['A',int2str(m),'XLabel']),16),',''', TextProp(k,:), ''');']);
  end
  for k=1:size(TextProp,1);
    eval(['Y',int2str(m),TextProp(k,:), '=get(',num2str(eval(['A',int2str(m),'YLabel']),16),',''', TextProp(k,:), ''');']);
  end                       
  for k=1:size(TextProp,1);
    eval(['Z',int2str(m),TextProp(k,:), '=get(',num2str(eval(['A',int2str(m),'ZLabel']),16),',''', TextProp(k,:), ''');']);
  end

end %for m

Dateiname = lower(Dateiname);             
PointPos = findstr(Dateiname,'.');
if PointPos==[]
  Dateiname = [Dateiname,'.fig'];
end

clear('ObjType','ObjHandle','AxesHandle','FigProp','AxesProp','LineProp','SurfProp', ...
      'PatchProp','ImageProp','TextProp','k','l','m','PointPos');
      
eval(['save ',Pfad, Dateiname]);  

Fid = fopen([Pfad, Dateiname],'r','ieee-be');
fseek(Fid,0,1);
Size = ftell(Fid);                     
fclose(Fid);
disp (['  ',int2str(Size),' bytes of figure no.',int2str(Handle), ' stored in:  ', Pfad, Dateiname]);
                      
                      
                      
                      
