为什么我的 MATLAB (R2008a) 对象的类会在保存/加载周期中发生变化?
更新: 这是一个已知的错误 - 链接需要登录 Mathworks来访问它。
错误报告摘要
MATLAB 用户定义的实例 使用以下命令将类保存到 MAT 文件中 版本 7.6 (R2008a) 可能无法加载 如果其属性值之一正确 是不同 MATLAB 的实例 类。
简而言之,Mathworks 报告先前保存的顶级自定义对象可能加载不正确(如下所述),并且在“保存”步骤中发生错误。 因此,MAT 文件内的数据已损坏。
根据我的经验,这似乎是间歇性的。 在一个数据分析应用程序中,我写出了 75 个 MAT 文件,其中 37 个在保存时出现了这种损坏:(
请小心用户定义的对象。我在保存时添加了以下测试,以确保数据没有损坏
save('MAT_file_name.mat');
tmp=load('MAT_file_name.mat');
if ~isa(tmp.bb,'superClass')
msgbox({'THE FILE WAS NOT SAVED PROPERLY', ...
' ', ...
[' MAT_file_name.mat',]})
end
原始问题
我使用的是 MATLAB 2008a。这个微妙的错误在 MATLAB-2009a 中得到了修复。 无论如何,按照我的两个类的定义方式,保存/加载循环会导致一个类(superClass)的变量被加载为我的第二个类(propClass)的变量。
示例 MATLAB (r2008a) 会话
>> bb=superClass;
>> whos
Name Size Bytes Class Attributes
bb 1x1 60 superClass
>> save
>> clear
>> clear classes
>> load
>> whos
Name Size Bytes Class Attributes
bb 1x1 60 propClass
加载 matlab.mat 后,变量 bb 神秘地从 superClass 更改为 propClass
Class: superClass
该类需要包含一个 propClass 类型的数组,这是它的 Naive 定义
classdef superClass<handle
properties(SetAccess=private)
a = propClass.empty % need to set this property as type propClass
% otherwise the addProp method throws an error
count=0;
end
methods
function self=superClass
%empty class definitionS
end
function addProp(self)
p = propClass;
self.count = self.count+1;
self.a(self.count)=p;
end
end
end
Class: propClass
PropClass 是超类使用的第二个类。 它的定义对于这个错误并不重要。
问题
那么,为什么在 MATLAB-R2008a 中加载操作后 superClass 会更改为 propClass 呢? 其次,如何更改 superClass 的定义以避免这种症状?
注意
我在编写的一个较大的类中遇到了这种症状,并缩小了问题的根源。 我知道它发生在上述 MATLAB 会话中,但似乎如果我将一个对象添加到 superClass 中的属性数组中,那么问题就会消失。 因此,如果我在保存之前调用 superClass.addProp,则不会发生从 superClass 到 propClass 的奇怪变化。
UPDATE:
This is a known bug - link requires a login to Mathworks to access it.
Summary of bug report
An instance of a MATLAB user-defined
class saved to a MAT file using
Version 7.6 (R2008a) might not load
properly if one of its property values
is an instance of a different MATLAB
class.
Briefly, Mathworks reports that a previously saved top level custom object may be loaded incorrectly (as described below) and that the error occurs on the SAVE step. So, the data is corrupted inside the MAT file.
From my experience this seems to be intermittent. In one data analysis application I wrote out of 75 MAT files 37 were saved with this corruption :(
Be careful with user defined objects. I added the following test on save to make sure that the data is not corrupted
save('MAT_file_name.mat');
tmp=load('MAT_file_name.mat');
if ~isa(tmp.bb,'superClass')
msgbox({'THE FILE WAS NOT SAVED PROPERLY', ...
' ', ...
[' MAT_file_name.mat',]})
end
Orginal Question
Here I am using MATLAB 2008a. This subtle bug is fixed in MATLAB-2009a.
Anyway, the way my two classes are defined, the save/load cycle causes the variables of one class (superClass) to be loaded as variables of my second class (propClass).
Example MATLAB (r2008a) Session
>> bb=superClass;
>> whos
Name Size Bytes Class Attributes
bb 1x1 60 superClass
>> save
>> clear
>> clear classes
>> load
>> whos
Name Size Bytes Class Attributes
bb 1x1 60 propClass
After loading matlab.mat, the variable bb has mysteriously changed from superClass to propClass
Class: superClass
This class needs to contain an array of type propClass and here is it's Naive definition
classdef superClass<handle
properties(SetAccess=private)
a = propClass.empty % need to set this property as type propClass
% otherwise the addProp method throws an error
count=0;
end
methods
function self=superClass
%empty class definitionS
end
function addProp(self)
p = propClass;
self.count = self.count+1;
self.a(self.count)=p;
end
end
end
Class: propClass
PropClass is a second class used by the super class. Its definition is not important for this bug.
Question
So, why is superClass being changed to propClass after the load operation in MATLAB-R2008a? Secondly, how can I change the definition of superClass to avoid this symptom?
Note
I ran into this symptom in a larger class I had written and narrowed down the source of the problem. I know it occurs in the MATLAB session described above but it seems that if I add an object to the property array in superClass then the problem disappears.
So, if I call superClass.addProp before saving then the strange change from superClass to propClass doesn't occur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个奇怪的问题! 我还没有遇到过类似的事情,但是您可以首先尝试的一件事是将属性的初始化移至构造函数:
我不确定它会产生任何效果,但这是我能想到的唯一方法尝试。 希望能帮助到你! =)
That's a strange problem! I haven't come across anything like that, but one thing you could try first is to move the initializations of the properties to the constructor:
I'm not sure it will have any effect, but that's the only thing I can think of to try. Hope it helps! =)