Robochameleon  v1.0
copyfig.m
1 %COPYFIG Create a copy of a figure, without changing the figure
2 %
3 % Examples:
4 % fh_new = copyfig(fh_old)
5 %
6 % This function will create a copy of a figure, but not change the figure,
7 % as copyobj sometimes does, e.g. by changing legends.
8 %
9 % IN:
10 % fh_old - The handle of the figure to be copied. Default: gcf.
11 %
12 % OUT:
13 % fh_new - The handle of the created figure.
14 
15 % Copyright (C) Oliver Woodford 2012
16 
17 function fh = copyfig(fh)
18 % Set the default
19 if nargin == 0
20  fh = gcf;
21 end
22 % Is there a legend?
23 if isempty(findall(fh, 'Type', 'axes', 'Tag', 'legend'))
24  % Safe to copy using copyobj
25  fh = copyobj(fh, 0);
26 else
27  % copyobj will change the figure, so save and then load it instead
28  tmp_nam = [tempname '.fig'];
29  hgsave(fh, tmp_nam);
30  fh = hgload(tmp_nam);
31  delete(tmp_nam);
32 end
33 return