Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab save Handle instance doesn't save its properties values

Tags:

matlab

I have created the following class, which inherits both from handle and enum.

classdef ShiftType < handle
%SHIFTTYPE Defines shift per frame
    properties
        shift = 0
        num_shifts = 0
    end
    enumeration
        LateralCst %in meters
        RadialCst % in radians
        RadialVar % in beam distance ratio
        LateralVar % Same. Lateral shift calculated at focus range.
    end
end

If I create an instance of ShiftType and use it within a script, everything goes well. But I realized that, if I save this instance into a .mat file and then load it, its properies are set to their default value (0). Here is an example to illustrate the problem:

>> shift_type = ShiftType.RadialVar;
>> shift_type.shift = 0.5;
>> shift_type.num_shifts = 4;
>> shift_type

shift_type = 

    RadialVar

>> shift_type.shift

ans =

    0.5000

>> save test.mat shift_type
>> clear all
>> load test.mat
>> shift_type

shift_type = 

    RadialVar

>> shift_type.shift

ans =

     0

How can I have the properties saved in the .mat files along with the ShiftType instance? Note that those properties are independant of the Enum type, so I don't want to just have a ShiftType(val) function and default values for each enum (such as LateralCst (1, 4)).

Thanks ahead!

like image 233
Cyril Avatar asked Jun 05 '26 06:06

Cyril


1 Answers

It is most probably a bug. The documentation says that the property set methods should be called when the object is loaded however if you add this method to your class:

    function set.shift(obj, shift)
        obj.shift = shift;
        disp('Set method called!');
    end

You'll see that it is not being called. If you remove the enumeration part of the class, it works fine. Appears that loading of enumeration types have their own specific handling which doesn't take care of other properties.

like image 156
Mohsen Nosratinia Avatar answered Jun 07 '26 22:06

Mohsen Nosratinia