MATLAB: How to store the properties of a figure in MATLAB, then reset them at a later time

getMATLABoutputsetstructureuser-definable

I want to store the properties of a figure in order to reset the figure's current configuration at a later time. For example, I would like to read my figure's properties using:
line
props = get(gcf);
Then, after having made some changes, I would like to reset all properties using:
fnames = fieldnames(props);
for n = 1:length(fnames)
set(gcf,fnames{n},props.(fnames{n}))
end
However, I obtain the following error:
??? Error using ==> set
Attempt to modify read-only figure property: 'BeingDeleted'.

Best Answer

The GET function returns properties that are read-only and cannot be set using the SET function.
The best method for storing a figure's properties, then resetting them at a later time, is to use the SAVEAS function to save the figure as a FIG-file.
saveas(gca, 'foo', 'fig');
Then, you can load this figure using the OPEN function:
open('foo.fig');
However, if you would like to store the properties in a variable, you can avoid the read-only properties when resetting the figure. The read-only properties of a figure are specified as:
readonly = {'BeingDeleted','CurrentCharacter','CurrentObject','FixedColors','Type'};
Then, the figure properties can be reset using:
props = rmfield(props, readonly);
fnames = fieldnames(props);
for n = 1:length(fnames)
set(gcf,fnames{n},props.(fnames{n}))
end