Why does the MATLAB feature class (R2008a) change in the save / load cycle?

UPDATE: This is a known bug - access to it requires a login for Mathworks.

Error Report Summary

An example of a user-defined MATLAB class stored in a MAT file using Version 7.6 (R2008a) may not load correctly if one of its property values ​​is an instance of another MATLAB class.

In short, Mathworks reports that a previously saved top-level user object may not load correctly (as described below) and that an error occurs in the SAVE step. Thus, the data is corrupted inside the MAT file.

In my experience, this seems choppy. In one data analysis application, I wrote from 75 MAT 37 files with this damage :(

Be careful with user-defined objects. I added the following save test to make sure 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

Common question

Here I am using MATLAB 2008a. This subtle bug has been fixed in MATLAB-2009a. In any case, as my two classes are defined, the save / load cycle causes the loading of variables of one class (superClass) as the variables of my second class (propClass).

MATLAB Session Example (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       

After loading matlab.mat, the bb variable mysteriously changed from superClass to propClass

Class: superClass

This class should contain an array of type propClass, and here it is. 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 the second class used by the superclass. Its definition does not matter for this error.

Question

, superClass propClass MATLAB-R2008a? -, , ?

, . , MATLAB, , , , . , superClass.addProp , superClass propClass .

+3
1

! , :

classdef superClass < handle
    properties (SetAccess = private)
        a
        count
    end
    methods
        function self = superClass
            self.a = propClass.empty;
            self.count = 0;
        end
        function addProp(self)
            p = propClass;
            self.count = self.count+1;
            self.a(self.count) = p;
        end
    end
end

, - , , , . , ! =)

+2

Source: https://habr.com/ru/post/1705313/


All Articles