function Handle = loadfig( LoadName )
%Handle = loadfig( FileName )
%         
%       LOADFIG restores a FIGURE stored by SAVEFIG.
%       The result is a Handle specifying the new FIGURE. If any error occures its -1.
%       This Version is not able to restore UICONTROL and UIMENU.
%       Here is an example:
%
%       >>h=loadfig('c:\my_path\my_fig')     
%
%       If no FileName is given, LOADFIG opens a file select box. Your last load path was stored
%       in LOADFIG.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 < 1
  if exist('loadfig.ini')
    eval('load loadfig.ini -mat');
  else
    disp('Couldn''t find LOADFIG.INI');
    LoadPfad=[pwd, '\'];
  end

  HomePath = [pwd, '\'];
  if exist('LoadPfad');
    eval(['cd ',LoadPfad(1,1:length(LoadPfad)-1)]);
  end;
  [LoadName,LoadPfad]=uigetfile('*.fig','Load Figure');
  eval(['cd ',HomePath(1,1:length(HomePath)-1)]);
  eval('save loadfig.ini LoadPfad');
else
  LoadPfad=('');
end

LoadName = LoadName;             
PointPos = findstr(LoadName,'.');
if PointPos==[]
  LoadName = [LoadName,'.fig'];
end
if LoadName
  eval(['load ', LoadPfad, LoadName,' -mat']);            
else
  Handle = -1;
  error('Figure file doesn''t exist!')
  break
end

Handle= figure;

LineProp = ...
       ['Color        ';...  
        'EraseMode    ';...  
        'LineStyle    ';...  
        'LineWidth    ';...  
        'MarkerSize   ';...  
        'ButtonDownFcn';...  
        'Clipping     ';...  
        'Interruptible';...  
        'UserData     ';...  
        'Visible      '];  

SurfProp = ...                      
       ['EdgeColor    ';...
        'EraseMode    ';...
        'FaceColor    ';...
        'LineStyle    ';...
        'LineWidth    ';...
        'MarkerSize   ';...
        'MeshStyle    ';...
        'ButtonDownFcn';...  
        'Clipping     ';...  
        'Interruptible';...  
        'UserData     ';...  
        'Visible      '];  

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

ImageProp = ...                      
       ['ButtonDownFcn';...  
        'Clipping     ';...  
        'Interruptible';...  
        'UserData     ';...  
        'Visible      '];  

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

for m=length(FigChildren):-1:1
  
  axes;            
  AxesHandle = get(Handle,'Children');

  eval(['lmax=length(A',int2str(m),'Children);']);

  for l=lmax:-1:1
    PreStr=['O',int2str(l),'A',int2str(m)];
    eval(['ObjType=',PreStr,'Type;']);
  
    if strcmp(ObjType,'line')
      if eval([PreStr,'Zdata'])==[];
        eval(['line( ',PreStr,'Xdata,',PreStr,'Ydata );']);
      else
        eval(['line( ',PreStr,'Xdata,',PreStr,'Ydata,',PreStr,'Zdata );']);
      end
      eval(['ObjHandle = get(',num2str(AxesHandle(1),16),',''Children'');']);
      for k=1:size(LineProp,1);
        eval(['set(',num2str(ObjHandle(1),16),',''',LineProp(k,:),''',',PreStr,LineProp(k,:),');'])
      end
  
    elseif strcmp(ObjType,'surface')
      eval(['surface( ',PreStr,'Xdata,',PreStr,'Ydata,',PreStr,'Zdata,',PreStr,'CData );']);
      eval(['ObjHandle = get(',num2str(AxesHandle(1),16),',''Children'');']);
      for k=1:size(SurfProp,1);
      eval(['set(',num2str(ObjHandle(1),16),',''',SurfProp(k,:),''',',PreStr,SurfProp(k,:),');'])
    end

    elseif strcmp(ObjType,'image')
      eval(['image( ',PreStr,'XData,',PreStr,'YData,',PreStr,'CData );']);
      eval(['ObjHandle = get(',num2str(AxesHandle(1),16),',''Children'');']);
      for k=1:size(ImageProp,1);
        eval(['set(',num2str(ObjHandle(1),16),',''',ImageProp(k,:),''',',PreStr,ImageProp(k,:),');'])
      end
  
    elseif strcmp(ObjType,'patch')
      if eval([PreStr,'CData'])==[];
        eval([PreStr,'CData=',PreStr,'FaceColor;']);
      end
      if eval([PreStr,'ZData'])==[];
        eval(['patch( ',PreStr,'XData,',PreStr,'YData,',PreStr,'CData );']);
      else
        eval(['patch( ',PreStr,'XData,',PreStr,'YData,',PreStr,'ZData,',PreStr,'CData );']);
      end
      eval(['ObjHandle = get(',num2str(AxesHandle(1),16),',''Children'');']);
      for k=1:size(PatchProp,1);
      eval(['set(',num2str(ObjHandle(1),16),',''',PatchProp(k,:),''',',PreStr,PatchProp(k,:),');'])
    end

    elseif strcmp(ObjType,'text')
      eval(['text( ',PreStr,'Position(1),',PreStr,'Position(2),',PreStr,'Position(3),',PreStr,'String );']);
      eval(['ObjHandle = get(',num2str(AxesHandle(1),16),',''Children'');']);
      for k=1:size(TextProp,1);
        eval(['set(',num2str(ObjHandle(1),16),',''',TextProp(k,:),''',',PreStr,TextProp(k,:),');'])
      end
  
    end %if strcmp...
  
  end %for l...
  
end %for m...


  
FigProp = ...
       ['BackingStore         ';...
        'Color                ';...
        'Colormap             ';...
        'MenuBar              ';...
        'MinColormap          ';...
        'Name                 ';...
        'NextPlot             ';...
        'NumberTitle          ';...
        'PaperUnits           ';...
        'PaperOrientation     ';...
        'PaperPosition        ';...
        'Pointer              ';...
        'Position             ';...
        'Resize               ';...
        'SelectionType        ';...
        'ShareColors          ';...
        'Units                ';...
        'WindowButtonDownFcn  ';...
        'WindowButtonMotionFcn';...
        'WindowButtonUpFcn    ';...
        'ButtonDownFcn        ';...
        'Clipping             ';...
        'Interruptible        ';...
        'UserData             ';...
        'Visible              '];
        
for k=size(FigProp,1):-1:1;
  eval(['set(Handle,''', FigProp(k,:),''', Fig', FigProp(k,:),');']);
end


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


for m=1:length(AxesHandle)

  for k=size(AxesProp,1):-1:1;
    eval(['set(',num2str(AxesHandle(m),16),',''',AxesProp(k,:),''',A',int2str(m),AxesProp(k,:),');'])
  end



  AxesTitle = get(AxesHandle(m),'Title');
  for k=1:size(TextProp,1);
    eval(['set(',num2str(AxesTitle,16),',''',TextProp(k,:),''',T',int2str(m),TextProp(k,:),');'])
  end
  
  AxesXLabel = get(AxesHandle(m),'XLabel');
  for k=1:size(TextProp,1);
    eval(['set(',num2str(AxesXLabel,16),',''',TextProp(k,:),''',X',int2str(m),TextProp(k,:),');'])
  end
  
  AxesYLabel = get(AxesHandle(m),'YLabel');
  for k=1:size(TextProp,1);
    eval(['set(',num2str(AxesYLabel,16),',''',TextProp(k,:),''',Y',int2str(m),TextProp(k,:),');'])
  end
  
  AxesZLabel = get(AxesHandle(m),'ZLabel');
  for k=1:size(TextProp,1);
    eval(['set(',num2str(AxesZLabel,16),',''',TextProp(k,:),''',Z',int2str(m),TextProp(k,:),');'])
  end
  
end %for m...  
  
  
if get(Handle,'NumberTitle')=='on';
  set(Handle,'Name', [LoadPfad, LoadName]);
end;  

                           
