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