Matlab save Handle实例不保存其属性值

时间:2016-05-18 07:51:56

标签: matlab

我创建了以下类,它继承了句柄和枚举。

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

如果我创建一个ShiftType实例并在脚本中使用它,一切顺利。但我意识到,如果我将此实例保存到.mat文件然后加载它,其属性将设置为其默认值(0)。这是一个说明问题的例子:

>> 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

如何将.mat文件中保存的属性与ShiftType实例一起保存? 请注意,这些属性与Enum类型无关,因此我不希望只有ShiftType(val)函数和每个枚举的默认值(例如LateralCst(1,4))。

提前致谢!

3 个答案:

答案 0 :(得分:1)

这很可能是一个错误。 The documentation表示在加载对象时应调用属性集方法,但是如果将此方法添加到类中:

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

你会看到它没有被调用。如果删除类的枚举部分,它可以正常工作。看起来加载枚举类型有自己的特定处理,而不处理其他属性。

答案 1 :(得分:1)

感谢您的回答。我已经向Matlab报告了这个错误。 与此同时,我已经通过将ShiftType分为两个类来解决了这个问题。 Shift类是具有可编辑属性的泛型类,其中一个属性是枚举类型before(ShiftType)的实例。

Sub Import(sheetname As Worksheet)

'Debug.Print (sheetname)

        cName = "Fund ID"
        cA = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "CUSIP"
    cB = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Description"
    cC = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Security Type"
    cD = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Currency"
    cE = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Price Date"
    cF = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Current Price"
    cG = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Prior Price"
    cH = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Change Price (%)"
    cI = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Change Price (%)"
    cJ = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "BPS Impact"
    cK = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Source"
    cL = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column
    cName = "Comment"
    cM = sheetname.Rows.Find(What:=UCase(cName), LookAt:=xlWhole, SearchDirection:=xlNext).Column



LastRow = Range("b65000").End(xlUp).Row
    For r = 2 To LastRow
            Row = Row + 1
                aRecon1(Row, 1) = CStr(Cells(r, cA)) 'Fund ID ' added string value method
               'aRecon1(Row, 1) = Cells(r, cA)            'Fund ID
                aRecon1(Row, 2) = Cells(r, cB)            'CUSIP
                aRecon1(Row, 3) = Cells(r, cC)            'Description
                aRecon1(Row, 4) = Cells(r, cD)            'Security Type
                aRecon1(Row, 5) = Cells(r, cE)            'Currency
                aRecon1(Row, 6) = Cells(r, cF)            'Price Date
                aRecon1(Row, 7) = Cells(r, cG).Value      'Current Price
                'Debug.Print (Cells(r, cG).Value)
                                                                                    Debug.Print (Cells(r, cG).Value)
                    'Debug.Print (Cells(r, cG))
                aRecon1(Row, 8) = Cells(r, cH).Value      'Prior Price
                aRecon1(Row, 9) = Cells(r, cI)            'Change Price (%)
                aRecon1(Row, 10) = Cells(r, cJ)           'BPS Impact
                aRecon1(Row, 11) = Cells(r, cK)           'Source
                                                                                    Debug.Print (Cells(r, cK))
                aRecon1(Row, 12) = Cells(r, cL)          


    Next r

Sheets("Macro").Activate

classdef Shift
%SHIFT Defines shift per frame
properties
    type = ShiftType.RadialVar;
    val = 0; % Ref ShiftType
    num_shifts = 0;
end
methods
    function obj = Shift(type, val, num_shift)
        obj.type = type;
        obj.val = val;
        obj.num_shifts = num_shift;
    end
end
end

我认为这是修复错误之前最直接的方法。 再次感谢您的回答:)

答案 2 :(得分:0)

这是我写的东西。我不会说完美。但是现在就完成了工作。类文件(相同类文件)中不需要更改。只需运行此代码

%% Initialise data
shift_type = ShiftType.RadialVar;
shift_type.shift = 0.5;
shift_type.num_shifts = 4;

%% save file
file_name='test11.mat'; % write filename
class_object='shift_type';
class_name=class(eval(class_object));
propNames=properties(class_name);
data=cell(1,size(propNames,1)+1);
data{1}=class_object;
for index=2:size(propNames,1)+1
    propertyName=strcat(class_object,'.',propNames{index-1});
    data{index}=[propNames(index-1) eval(propertyName)];
end
save(file_name,class_object,'data');

%% clear workspace
clear all;

%% load file
file_name='test11.mat'; %read filename
load(file_name);
for index=2:size(data,2)
    property=data{index};
    propertyName=strcat(data{1},'.',property{1});
    expresn=strcat(propertyName,'=',num2str(property{2}),';');
    eval(expresn);
end

%% Display data
shift_type.shift
shift_type.num_shifts