How do I prevent struct2handle from failing after a call to clear classes?
4 次查看(过去 30 天)
显示 更早的评论
Here is some perfectly good-looking code that should save a simple figure as a struct, load it from file and re-display the original figure.
figHndle = figure();
plot(1:10);
figStruct = handle2struct(figHndle);
save('FigureStruct.mat','figStruct');
close(figHndle);
clear;
clear classes;
load('FigureStruct.mat');
figHndle = struct2handle(figStruct,0,'convert');
This last line throws the following warning: "Warning: Unrecognized object type: graph2d.lineseries" and the displayed figure has no line, it just shows an empty axes. If I don't clear the classes this all works just fine. However, when developing classes in MATLAB, we all know that making such a call is often required. Furthermore, matlab implicitly calls clear classes every time it re-starts. Saving to a .fig file is not an option for me. Also, if I call
f = figure('Visible','off');plot(1:2);close(f)
Directly after loading FigureStruct.mat, everything works fine.
I assume that the call to 'clear classes' is removing all the plotting-related classes from memory and so by the time the struct is plotted graph2d.lineseries is missing and matlab freaks out. I just don't know how to prevent that from happening. Does anyone know how to prevent this from happening?
0 个评论
回答(1 个)
Sean de Wolski
2013-12-16
编辑:Sean de Wolski
2013-12-16
Any reason to avoid hgload and hgsave?
figHndle = figure();
plot(1:10);
hgsave(figHndle,'FigureStruct.mat');
close(figHndle);
clear;
clear classes;
hgload('FigureStruct.mat');
Also, hgsave-ing to a mat file will give you a saved struct that you can load with load() but I still see the line series warning with struct2handle.
3 个评论
Sean de Wolski
2013-12-16
编辑:Sean de Wolski
2013-12-16
Interesting. I'm not sure I'd completely give up on hgload/|hgsave| yet.
First, since the file is written as a MAT file, you could use the MATFILE class to append the other class specific data in to the file containing the struct written by hgsave. (Though I don't think this is giving you much benefit over handle2struct as the warning is in the struct2handle).
The loadobj method would load just the structure from the matfile using hgload for all of the figure and regular load for everything else.
delete FigureStruct.mat
figHndle = figure();
plot(1:10);
hgsave(figHndle,'FigureStruct.mat','-v7.3');
x = matfile('FigureStruct','writable',true);
figHndle = figure;
surf(peaks);
drawnow;
x.hgS_070000(1,2) = handle2struct(figHndle); %add second figure
x.A = pi;
x.S = struct('HelloWorld','Hello World!');
close all;
clear;
clear classes;
hgload('FigureStruct.mat'); %figure
S = load('FigureStruct.mat'); %everything else
Since you're worried about figures closing, you could always notify in the close request function that it's about to close and have your class listen for this and call handle2struct at this point, storing it for later use.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!