forked from altmany/export_fig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyfig.m
59 lines (56 loc) · 2.04 KB
/
copyfig.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function fh = copyfig(fh)
%COPYFIG Create a copy of a figure, without changing the figure
%
% Examples:
% fh_new = copyfig(fh_old)
%
% This function will create a copy of a figure, but not change the figure,
% as copyobj sometimes does, e.g. by changing legends.
%
% IN:
% fh_old - The handle of the figure to be copied. Default: gcf.
%
% OUT:
% fh_new - The handle of the created figure.
% Copyright (C) Oliver Woodford 2012, Yair Altman 2015
% 26/02/15: If temp dir is not writable, use the dest folder for temp
% destination files (Javier Paredes)
% 15/04/15: Suppress warnings during copyobj (Dun Kirk comment on FEX page 2013-10-02)
% 09/09/18: Fix issue #252: Workaround for cases where copyobj() fails for any reason
% Set the default
if nargin == 0
fh = gcf;
end
% Is there a legend?
useCopyobj = isempty(findall(fh, 'Type', 'axes', 'Tag', 'legend'));
if useCopyobj
% Safe to copy using copyobj
oldWarn = warning('off'); %Suppress warnings during copyobj (Dun Kirk comment on FEX page 2013-10-02)
try
fh = copyobj(fh, 0);
catch
% Fix issue #252: Workaround for cases where copyobj() fails for any reason
useCopyobj = false; % if copyobj() croaks, use file save/load below
end
warning(oldWarn);
end
if ~useCopyobj
% copyobj will change the figure, so save and then load it instead
tmp_nam = [tempname '.fig'];
try
% Ensure that the temp dir is writable (Javier Paredes 26/2/15)
fid = fopen(tmp_nam,'w');
fwrite(fid,1);
fclose(fid);
delete(tmp_nam); % cleanup
catch
% Temp dir is not writable, so use the current folder
[dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
fpath = pwd;
tmp_nam = fullfile(fpath,[fname fext]);
end
hgsave(fh, tmp_nam);
fh = hgload(tmp_nam);
delete(tmp_nam);
end
end