How do I prevent struct2handle from failing after a call to clear classes?

3 views (last 30 days)
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?

Answers (1)

Sean de Wolski
Sean de Wolski on 16 Dec 2013
Edited: Sean de Wolski on 16 Dec 2013
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 Comments
Sean de Wolski
Sean de Wolski on 16 Dec 2013
Edited: Sean de Wolski on 16 Dec 2013
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.
KARL BEUTNER
KARL BEUTNER on 16 Dec 2013
Edited: KARL BEUTNER on 16 Dec 2013
I still don't think this will work. Here is a dummy example of what my top-level script looks like:
report = FigureReport();
report.Title = 'Demo Report';
report.Data = <some data>;
f1 = figure();
plot(1:1:10);
figExtPos = myFigExtension(f1);
report.Add(figExtPos);
f2 = figure();
plot(10:-1:1);
figExtNeg = myFigExtension(f2);
report.Add(figExtNeg);
save('MyDemoReport.mat','report');
Notice, I am directly saving the report, which will in-turn save myFigExtension, which will in-turn save the figure. I don't have direct knowledge/access to the matfile that is being written to.
I want struct2handle to work properly and as described. Currently, I would like to log a bug against struct2handle. Do you know how I go about doing this, and what the timeline is for getting a fix?

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!