function hgsave(h, filename)
% HGSAVE  Saves an HG object hierarchy to a MAT file
% 
% HGSAVE(H, 'Filename') saves the object identified by 
% the handle H to a file name 'Filename.'  If 'filename' 
% contains no extension, then the extension '.fig' is added.
%
% See also HGLOAD
%

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 1998/11/18 19:58:30 $
%   D. Foti  11/10/97

if any(~ishandle(h))
    error('Invalid handle');
end

[path, file, ext] = fileparts(filename);

% fileparts returns everything from the last . to the end of the
% string as the extention so the following test will catch
% an extention with 0, 1, or infinity dots.
% for example, all these filenames will have .fig added to the end:
%  foo.
%  foo..
%  foo.bar.
%  foo.bar...

if isempty(ext) | strcmp(ext, '.')
  filename = fullfile(path, [file , ext, '.fig']);
end

% Revision encoded as 2 digits for major revision,
% 2 digits for minor revision, and 2 digits for
% patch revision.  This is the minimum revision 
% required to fully support the file format.
% e.g. 050200 means 5.2.0
hgS_050200 = handle2struct(h);
save(filename, 'hgS_050200');
