フィルターのクリア

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

2 ビュー (過去 30 日間)
KARL BEUTNER
KARL BEUTNER 2013 年 12 月 13 日
編集済み: KARL BEUTNER 2013 年 12 月 16 日
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?

回答 (1 件)

Sean de Wolski
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
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.
KARL BEUTNER
KARL BEUTNER 2013 年 12 月 16 日
編集済み: KARL BEUTNER 2013 年 12 月 16 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by