简体   繁体   English

Matlab从“ .mat”文件中“加载”图形对象打开绘图窗口

[英]Matlab 'load' figure objects from '.mat' file opens plot window

Why does MATLAB open a plot window when I load a .mat file that contains a figure inside an struct ? 为什么在加载包含struct中的图形的.mat文件时MATLAB会打开绘图窗口?

The problem that I am facing is that I have the output of an optimization algorithm as a collection of .mat files. 我面临的问题是我将优化算法的输出作为.mat文件的集合。 Each .mat file contains the state of each generation of the algorithm in a form of a single struct . 每个.mat文件都以单个struct的形式包含算法每一代的状态。 The state structure has (among other variables) a field of type matlab.ui.Figure . 状态结构(除其他变量外)具有类型为matlab.ui.Figure的字段。 Now, whenever I try to load any of these files with the load command, a plot window opens automatically. 现在,每当我尝试使用load命令load这些文件中的任何一个时,都会自动打开一个绘图窗口。

MATLAB工作区

Is there any way to stop MATLAB from from opening this plot window? 有什么方法可以阻止MATLAB打开该绘图窗口?

I am using MATLAB 2015rb. 我正在使用MATLAB 2015rb。

The reason that it is displaying a figure is because if you look closely at your state structure, there is a figure object stored in there. 之所以显示图形,是因为如果您仔细查看state结构,就会在其中存储figure对象。 When you load this graphics object (or any object, really) from a file, MATLAB will reconstruct the object. 当从文件中加载该图形对象(或实际上是任何对象)时,MATLAB将重建该对象。 The defined behavior for loading a figure (it's loadobj method) is to open the figure. 加载图形的定义行为(它是loadobj方法)是打开图形。

This is a recent issue because older versions of MATLAB stored graphics handles as simply a number and when loading a graphics handle from file, MATLAB had no way of knowing that it was supposed to be a figure so it would just parse it as a number and move on without displaying a new figure window. 这是一个新问题,因为较旧版本的MATLAB将图形句柄存储为简单的数字,并且从文件加载图形句柄时,MATLAB无法知道它应该是数字,因此只能将其解析为数字并继续而不显示新的图形窗口。

Unfortunately since your figure handle is nested within a struct there is no easy way to not load it. 不幸的是,由于您的figure手柄嵌套在struct ,因此没有简单的方法可以加载它。 Probably the easiest thing to do would be to just delete the figure object right after loading the file (since you have the handle already). 可能最简单的方法是在加载文件后立即删除图形对象(因为您已经拥有了句柄)。

data = load('filename.mat', 'state');
delete(data.state.hFigure);

And if you really don't like the figure poping up even for a second, you can set the default figure Visible property to 'off' prior to loading and then reset it afterwards. 而且,如果您真的不希望该图弹出一秒钟,则可以在加载之前将默认的图Visible性属性设置为'off' ,然后再将其重置。

% Determine what the visibility was
prev = get(0, 'DefaultFigureVisible');

% Make it so figures don't appear
set(0, 'DefaultFigureVisible', 'off')

% Load data and delete the figure
data = load('filename.mat', 'state');
delete(state.hFigure);

% Reset the visibility
set(0, 'DefaultFigureVisible', prev)

Another potential solution (which would not require you to know where the figure handles are in your struct) is to overwrite the DefaultFigureCreateFcn to simply delete any figure that is created. 另一个可能的解决方案(不需要您知道figure句柄在结构中的位置)是覆盖DefaultFigureCreateFcn以简单地删除所创建的任何图形。

% After this point you can't create any figures or they will delete themselves
set(0, 'DefaultFigureCreateFcn', @(s,e)delete(s))

% Load your data (no figures!)
load('filename.mat', 'state')

% Allow figures to be created again
set(0, 'DefaultFigureCreateFcn', '')

In the future, to avoid this behavior, consider not saving any graphics handles within your .mat files. 将来,为避免发生这种情况,请考虑不要在.mat文件中保存任何图形句柄。 They are very large objects and MATLAB actually will issue a warning when saving one to a file as it is not recommended. 它们是非常大的对象,并且不建议将其保存到文件中时,MATLAB实际上会发出警告。

Warning: Figure is saved in test.mat. 警告:图形保存在test.mat中。 Saving graphics handle variables can cause the creation of very large files. 保存图形句柄变量可能会导致创建非常大的文件。 To save graphics figures, use savefig . 要保存图形,请使用savefig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM