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

조회 수: 37 (최근 30일)
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
Ameer Hamza
Ameer Hamza 2020년 10월 7일
This error will happen if any of the figures is closed. If all figure windows are open, it should work fine.

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 7일
Try this to only save figures that are currently open
idx = arrayfun(@ishandle, fig);
fig_new = fig(idx);
savefig(fig, 'name.fig');
  댓글 수: 4
Joan Vazquez
Joan Vazquez 2020년 10월 7일
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!
Ameer Hamza
Ameer Hamza 2020년 10월 7일
I am glad to be of help! Yes, mathworks have made many improvements during recent releases. An upgrade will be a good option.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by