Search code examples
matlabrecover

Variable YYY originally saved as a XXX cannot be instantiated as an object and will be read in as a uint32


I've made a mistake and recorded a bunch of important data using a class definition that I can't find anymore. The methods are not important, I just want the data; a struct conversion would be fine.

Is there any way I can recover this?

Googling it did not help.


Solution

  • The solution is to create a new class that overloads the loadobj method. See here for more information regarding the load process for classes.

    I replicated your problem by creating a class:

    classdef myclass
       properties
          Prop1
          Prop2
          Prop3
       end
       methods
           function obj = myclass(a,b,c)
               obj.Prop1 = a;
               obj.Prop2 = b;
               obj.Prop3 = c;
           end
       end
    end
    

    Then I created an object of this class and saving it to a file "x.mat":

    x = myclass('a',56,[1,2,3]);
    save x
    

    Next, I deleted the myclass file and did clear classes. This put me in your situation.

    I then created a new myclass class that overloads the loadobj method:

    classdef myclass
       properties
           data
       end
       methods (Static)
           function obj = loadobj(s)
               obj = myclass;
               obj.data = s;
           end
       end
    end
    

    Note how it doesn't know any of the original properties. This does not matter. If any of the original properties is missing when loading an object from a MAT-file, loadobj will be called with s being a struct containing all the properties of the original object.

    With this new class definition, load x created an object x of class myclass, where x.data was a struct containing the properties of the object saved in "x.mat".