【问题标题】:Matlab save Handle instance doesn't save its properties valuesMatlab保存句柄实例不保存其属性值
【发布时间】:2016-05-18 07:51:56
【问题描述】:

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

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

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

先谢谢了!

【问题讨论】:

  • 我看不到任何解决此问题的方法,枚举无法重载saveobloadobj,而且我不知道有任何替代方法来操纵保存/加载过程。我建议联系支持,这绝对是一个错误。

标签: matlab


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    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
    

    还有:

    classdef ShiftType
    %SHIFTTYPE Defines shift type
    enumeration
        LateralCst %in meters
        RadialCst % in radians
        RadialVar % in beam distance ratio(1 = moves from one beam to another).
        LateralVar % Same. Lateral shift calculated at focus range.
    end
    end
    

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

    【讨论】:

      【解决方案3】:

      这是我写的。我不会说它完美。但现在可以完成工作。类文件(相同的类文件)无需更改。只需运行此代码

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

      【讨论】:

      • 感谢您的解决方案。不过我不会使用它,因为我认为如果在代码中多次保存/加载它太麻烦了。我已经提出了另一个需要第二个类文件的解决方案,但是在处理它的任何实例时都没有问题。
      猜你喜欢
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多