The reason that it is displaying a figure is because if you look closely at your state
structure, there is a figure
object stored in there. When you load this graphics object (or any object, really) from a file, MATLAB will reconstruct the object. The defined behavior for loading a figure (it's loadobj
method) is to open the figure.
This is a recent issue because older versions of MATLAB stored graphics handles as simply a number and when loading a graphics handle from file, MATLAB had no way of knowing that it was supposed to be a figure so it would just parse it as a number and move on without displaying a new figure window.
Unfortunately since your figure
handle is nested within a struct
there is no easy way to not load it. Probably the easiest thing to do would be to just delete the figure object right after loading the file (since you have the handle already).
data = load('filename.mat', 'state');
delete(data.state.hFigure);
And if you really don't like the figure poping up even for a second, you can set the default figure Visible
property to 'off'
prior to loading and then reset it afterwards.
% Determine what the visibility was
prev = get(0, 'DefaultFigureVisible');
% Make it so figures don't appear
set(0, 'DefaultFigureVisible', 'off')
% Load data and delete the figure
data = load('filename.mat', 'state');
delete(state.hFigure);
% Reset the visibility
set(0, 'DefaultFigureVisible', prev)
Another potential solution (which would not require you to know where the figure
handles are in your struct) is to overwrite the DefaultFigureCreateFcn
to simply delete any figure that is created.
% After this point you can't create any figures or they will delete themselves
set(0, 'DefaultFigureCreateFcn', @(s,e)delete(s))
% Load your data (no figures!)
load('filename.mat', 'state')
% Allow figures to be created again
set(0, 'DefaultFigureCreateFcn', '')
In the future, to avoid this behavior, consider not saving any graphics handles within your .mat files. They are very large objects and MATLAB actually will issue a warning when saving one to a file as it is not recommended.
Warning: Figure is saved in test.mat. Saving graphics handle variables can cause the creation of very large files. To save graphics
figures, use savefig
.