Save figure array error: "H must be an array of handles to valid figures"

I have a custom method from an internal codebase that creates a bunch of figures.
I have all the figures open in Matlab and I would like to save them. However, this gives and error:
fig = CustomStruct.CreateFigureCustomMethod;
savefig(fig, 'name.fig');
"Error using savefig (line 43)
H must be an array of handles to valid figures."
But fig is actually a Figure array in the workspace!
I know this may be impossible to answer due to reproducibilty, but I wonder if there is another way to save all open figures.
I could dig into the method and modify it, but I have the same issue with several methods and I do not think this should be necessary.

1 commentaire

This error will happen if any of the figures is closed. If all figure windows are open, it should work fine.

Connectez-vous pour commenter.

 Réponse acceptée

Try this to only save figures that are currently open
idx = arrayfun(@ishandle, fig);
fig_new = fig(idx);
savefig(fig, 'name.fig');

4 commentaires

Thanks to your comment I realized that the idx is a 1x18 array of zeros, and I guess that is is related to the fact that I am working with a Live Script. The figures appear correctly in the Live Script, but they are "grey" in the open external figures.
This is why I wanted to catch them in the "fig" object to save them, but I guess it is not possible with Live Scripts.
Thanks
(also, for other users, shouldn't your last line be savefig(fig_new, 'name.fig'); ?
Yes, you are right. Thanks for pointing out. The saveas line should have fig_new. I have updated it.
The problem might be related to the Live script. You are using R2019a, and the live script had fewer features back then. In R2020a, I am able to save the figure from live script using their handles. For example, the following works
f(1) = figure();
ax = axes(f(1));
plot(ax, rand(1,10));
f(2) = figure();
ax = axes(f(2));
plot(ax, rand(1,10));
f(3) = figure();
ax = axes(f(3));
plot(ax, rand(1,10));
The above code runs in the live script, and the following line works fine.
saveas(f, 'filename.fig')
However, the filename.fig can only be opened in Live script. You cannot use double-click or openfig() from the command window to load this file.
Two things, for completeness of this post:
1) Your code threw an error with 2019a [Error using saveas (line 179) Unsupported format) or extension: fig]. However, using savefig(f, 'filename.fig') worked fine, so the issue must be in the way my figures are generated or stored.
2) For some misterious reason, looping through the array in the following way worked:
figs = CustomStruct.CreateFigureCustomMethod;
for i=1:numel(figs)
fig = figs(i);
saveas(fig.handle, 'filename', 'fig');
end
My best guess is that the figures are somehow custom classes...
Accepting your answer. Time to push my department for a Matlab upgrade!
I am glad to be of help! Yes, mathworks have made many improvements during recent releases. An upgrade will be a good option.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2019a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by