function f = openfig (filename)
    fig_struct = load('-mat',filename);
    f = create_handle_from_struct(fig_struct.hgS_070000);
end


function h = create_handle_from_struct(s, par)
  is_unsupported_handle_type = 0;

  % Create the appropriate handle based on the type in the struct
  if strcmp(s.type, 'figure')
      h = figure; 
  elseif strcmp(s.type, 'axes')
      h = axes;
  elseif strcmp(s.type, 'graph2d.lineseries')
      h = line;
  elseif strcmp(s.type, 'text')
      h = text;
  else
      is_unsupported_handle_type = 1;
      fprintf(1,'Unsupported handle type: %s\n', s.type);
  end

  % Only try to set the corresponding properties if it is a handle
  % type that this script currently supports
  if ~is_unsupported_handle_type
      
      % Iterate through all the properties of the saved figure and
      % apply them to the corresponding properties in the new
      % figure
      struct_fields = lower(fieldnames (s.properties));
      struct_values = struct2cell (s.properties);
      figure_fields = lower(fieldnames (get(h)));
      for I = 1:length(struct_fields)
          if isprop(h, struct_fields{I})
              set(h,struct_fields{I}, struct_values{I});
          end
      end

      % If this sub-struct has a non-empty 'special'
      % field, it needs to be handled differently.
      % As far as I can tell from testing and inspecting the struct
      % from a FIG file generated by Matlab, for axes the special field contains
      % the index of the children that make up the [title xlabel
      % ylabel zlabel] in that order. So, these items shouldn't be
      % simply added as children, but should be set to the
      % appropriate property in the axes.
      % Hopefully there aren't a whole bunch of undocumented uses
      % of this special field.

      for I=1:length(s.children)
          h_child = create_handle_from_struct(s.children(I), h); 
          
          % Determine if the special field indicate
          idx = find(s.special == I);          
          if isempty(idx)
              set(h_child, 'parent', h);
          elseif idx == 1 % title
              set(h, 'title', h_child);
          elseif idx == 2 % xlabel
              set(h, 'xlabel', h_child);
          elseif idx == 3 % ylabel
              set(h, 'ylabel', h_child);
          elseif idx == 4 %zlabel
              set(h, 'zlabel', h_child);
          else
             disp('Error dealing with special fields') 
          end
          
          keyboard
         
          
          
      end

  end
end