Figure Visibility and Save
307 views (last 30 days)
Show older comments
Hi everyone,
I'm plotting to an "invisible" figure and I'm almost positive it's working correctly.
I save the figure both as a 'fig' and as a 'jpg'.
When I go to open the 'jpg' format, the figure opens just as I would expect it to and I can see the plots.
However, when I try to access the 'fig' format, nothing opens up. I believe this is because the "visible" setting is still set to "off".
This is obviously a problem.
Is there any way to set the visibility setting to "on" during saving? (WITHOUT ever having the figure open up?)
I'd still like to keep the image's visibility to "off" while running my code.
Thanks!
JF
Accepted Answer
Thomas
on 11 Jul 2012
a=1:4;
f = figure('visible','off');
plot(a)
saveas(f,'newout','fig')
Now to reopen the figure
% to open the figure
openfig('newout.fig','new','visible')
3 Comments
Thomas
on 11 Jul 2012
AS far as I know this can be done only from the command window as you cannot set the newfigure visibility to on while openign it from windows explorer.
Please acccept my answers if it solved your problem.
Alessandro La Chioma
on 27 Oct 2015
I wrote a little function to open these invisible figures with a dialog box:
function open_InvisibleFig
init_dir = pwd;
FigToOpen = cellstr(uigetfile('*.fig','Select the Figures to open...', init_dir, 'MultiSelect','on') );
for ind = 1 : length(FigToOpen)
openfig(FigToOpen{ind},'new','visible')
end
More Answers (3)
Eric Salemi
on 25 Jun 2014
I recently solved this issue by directly modifying the saved figure:
% h is the variable containing
% the invisible figure handle
hgsave(h,filename);
map = load(filename,'-mat');
names = fieldnames(map);
for j = 1 : numel(names)
map.(names{j}).properties.Visible = 'on';
end
save(filename,'-struct','map');
Now, whenever you open the figure (command-line, explorer, etc.) it will be visible.
3 Comments
Igor Varfolomeev
on 12 Nov 2015
Indeed, this was a nice approach, though, it seem to not work in R2015b (probably, since R2014b, which introduced those hg2 graphics ..)
The variables, stored in the mat file are changed, but when you later load your figure "as figure" - it is still invisible. May be there's some "one more copy of the same properties"... Not sure.
Here's my test script:
function test_hgsave_invis2vis_v3_figv7( )
% Tests matfile()-based approach to figure editing, '-v7.3' case.
% The aim is to change figure 'Visible' property to 'on', without actually
% showing it.
% However, it fails - the figure, once loaded, is still invisible.
% Even though properties we've modified were actually modified OK.
% Tested in R2015b.
%%create test figure
hf = figure('visible','off');
ha = axes('Parent',hf);
hp = plot(ha,1:10,(1:10).^2); %#ok<NASGU>
% naive hgsave use, resulting in invisible figure
savefig(hf,'test_invisible.fig');
% The following save approaches work just the same:
% hgsave(hg,'test_invisible.fig','-v7.3');
%%modify mat file without loading all vars to RAM
map = load('test_invisible.fig','-mat');
names = fieldnames(map);
for j = 1:numel(names)
if ~isempty(map.(names{j})) % "if" is required to handle hgS_080000,
% see test_hgsave_invis2vis_v2_figv8
map.(names{j}).properties.Visible = 'on';
end
end
save('test_invisible.fig','-struct','map');
%%verify by loading
hf2=openfig('test_invisible.fig');
assert(strcmp(hf2.Visible,'off')); % figure is still invisible :(
% ...even though those props were set correctly:
map2 = load('test_invisible.fig','-mat');
assert(strcmp(map2.hgS_070000.properties.Visible,'on'));
assert(strcmp(map2.hgM_070000.properties.Visible,'on'));
end
Just the same for v8 figure format:
function test_hgsave_invis2vis_v3_figv8( )
% Tests matfile()-based approach to figure editing, 'compact' case.
% The aim is to change figure 'Visible' property to 'on', without actually
% showing it.
% However, it fails - the figure, once loaded, is still invisible.
% Even though properties we've modified were actually modified OK.
% Tested in R2015b.
%%create test figure
hf = figure('visible','off');
ha = axes('Parent',hf);
hp = plot(ha,1:10,(1:10).^2); %#ok<NASGU>
% naive hgsave use, resulting in invisible figure
savefig(hf,'test_invisible.fig','compact');
%%modify mat file without loading all vars to RAM
map = load('test_invisible.fig','-mat');
names = fieldnames(map);
for j = 1:numel(names)
if ~isempty(map.(names{j})) % "if" is required to handle hgS_080000,
% see test_hgsave_invis2vis_v2_figv8
map.(names{j}).properties.Visible = 'on';
end
end
save('test_invisible.fig','-struct','map');
%%verify by loading
hf2=openfig('test_invisible.fig');
assert(strcmp(hf2.Visible,'off')); % figure is still invisible :(
% ...even though those props were set correctly:
map2 = load('test_invisible.fig','-mat');
assert(isempty(map2.hgS_080000));
assert(strcmp(map2.hgM_080000.properties.Visible,'on'));
end
Igor Varfolomeev
on 12 Nov 2015
I've also tried a bit different approach, matfile() - based.
In this case figure does get visible. But.. it's also visible in the "modification" process:
function test_hgsave_invis2vis_v2_figv7( )
% Tests matfile()-based approach to figure editing, '-v7.3' case.
% The aim is to change figure 'Visible' property to 'on', without actually
% showing it. However, it fails - the figure is shown.
% Tested in R2015b.
%%create test figure
hf = figure('visible','off');
ha = axes('Parent',hf);
hp = plot(ha,1:10,(1:10).^2); %#ok<NASGU>
% naive hgsave use, resulting in invisible figure
hgsave(hf,'test_invisible.fig','-v7.3');
%%modify mat file without loading all vars to RAM
m = matfile('test_invisible.fig','Writable',true);
x=m.hgS_070000(1,1); % '070000' corresponds to '-v7.3'
x.properties.Visible='on';
m.hgS_070000(1,1) = x;
x=m.hgM_070000(1,1);
y=x.GraphicsObjects(1,1);
z=y.Format3Data(1,1);
z.Visible='on'; % trick is - here figure becomes visible.
% Note:
% This command:
% >> m.hgM_080000(1,1).GraphicsObjects(1,1).Format3Data(1,1).Visible='on';
% generates the following error:
% Cannot index into 'hgM_080000' because MatFile objects only
% support '()' indexing.
delete(m); % fclose();
end
Just the same for v8 figure file format:
function test_hgsave_invis2vis_v2_figv8( )
% Tests matfile()-based approach to figure editing, 'compact' case.
% The aim is to change figure 'Visible' property to 'on', without actually
% showing it. However, it fails - the figure is shown.
% Tested in R2015b.
%%create test figure
hf = figure('visible','off');
ha = axes('Parent',hf);
hp = plot(ha,1:10,(1:10).^2); %#ok<NASGU>
% naive hgsave use, resulting in invisible figure
savefig(hf,'test_invisible.fig','compact');
%%modify mat file without loading all vars to RAM
m = matfile('test_invisible.fig','Writable',true);
assert(isempty(m.hgS_080000)); % this prop is empty in 'compact' case
x=m.hgM_080000(1,1); % '080000' corresponds to 'compact'
% Warning: The file 'w:\Tfit\Matlab\SavedFigures\SaveFigure_savefig(compact)
% (naive).fig' was saved in a format that does not support partial loading.
% Temporarily loading variable 'hgM_080000' into memory. To use partial loading
% efficiently, save MAT-files with the -v7.3 flag.
% > In matlab.io.MatFile/inefficientPartialLoad (line 144)
% In matlab.io.MatFile/subsref (line 462)
%
y=x.GraphicsObjects(1,1);
z=y.Format3Data(1,1);
z.Visible='on'; % trick is - here figure becomes visible.
% Note:
% This command:
% >> m.hgM_080000(1,1).GraphicsObjects(1,1).Format3Data(1,1).Visible='on';
% generates the following error:
% Cannot index into 'hgM_080000' because MatFile objects only
% support '()' indexing.
delete(m); % fclose();
end
Baha
on 14 Oct 2015
Edited: Baha
on 14 Oct 2015
This code below still doesn't work for me. When I click on the figure, it still doesn't open up.
Anyone was able to turn the visibility of the figure on without displaying it?
filename = ['PeaksFile0',num2str(111),'.fig'];
h = figure('visible', 'off');
surf(peaks);
savefig(filename)
hgsave(h,filename);
map = load(filename,'-mat');
names = fieldnames(map);
for j = 1 : numel(names)
map.(names{j}).properties.Visible = 'on';
end
save(filename,'-struct','map');
1 Comment
Igor Varfolomeev
on 13 Nov 2015
Yep, it looks like this method got obsolete, and is not working in R2015a and R2015b.
Igor Varfolomeev
on 13 Nov 2015
If anyone is still looking for an answer, take a look at this post - I've just tried to collect all known workarounds in a single post.
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!