MATLAB: After saving the figure to a FIG file, why do I get an error about an “Invalid field name component” when trying to open it

characterdashfigfiguregetappdatahyphenloadMATLABmultiplenamesavedsecondtwice

I am able to open the saved figure the first time, but I get an error on the second try.
Using OPENFIG,
hf = figure;
surf(peaks);
drawnow
hgsave(hf,'test1-bad.fig');
close(hf)
hf=openfig('test1-bad.fig')
close(hf)
hf=openfig('test1-bad.fig')
I get the following error:
??? Error using ==> subsref
Invalid field name component.
Error in ==> D:\Applications\MATLABR12p1\toolbox\matlab\graphics\getappdata.m
On line 27 ==> value = subsref(value,struct('type','.','subs',name));
Error in ==> D:\Applications\MATLABR12p1\toolbox\matlab\graphics\openfig.m
On line 50 ==> figs = getappdata(0, TOKEN);
Using OPEN,
hf = figure;
surf(peaks);
drawnow
hgsave(hf,'test2-bad.fig');
close(hf)
hf=open('test2-bad.fig')
close(hf)
hf=open('test1-bad.fig')
I get the following error:
??? Error using ==> open
Invalid field name component.

Best Answer

This bug has been fixed for Release 14 SP1 (R14SP1). For previous releases, read below for any possible workarounds:
This is a bug in MATLAB 6.5 (R13) and previous versions.
The problem is related to the characters used to name the FIG file. As a general rule, any file name that passes the ISVARNAME test will not cause this problem. see the documentation on ISVARNAME for more information.
As a workaround, you can rename the FIG file such that it passes the ISVARNAME test, and restart MATLAB. You will then be able to open the file without any problems.