Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
save and load an object having events defined

Subject: save and load an object having events defined

From: meng long

Date: 20 Jun, 2009 11:52:01

Message: 1 of 1

classdef HLstd1 < handle % Hidden=false, Sealed = false
% @HLstd1/HLstd1 日期标杆。
%
% 使用说明:
% oObj = HLstd1();
% oObj = HLstd1(dLstd,d8Date);
%
% Problem for case 1:
% I hope that the properties ('dNoRows' etc.) can be calculated
% automatically, when 'dLstd' is set. So I have writed the code
% as in this .m file. When the code is used in Matlab 2009a,
% the coming warning occurs. Since the properties ('dNoRows' etc.)
% are used frequently, it is not a fine case for defining them as
% Dependent property.
%
% Warning:
% If a class has a non-Dependent property set method that references
% another property in the class, it is possible to create a subtle error.
% The problem occurs when you use save to save objects of the class and
% then use load to load them. When load calls the set method to set the
% property value, it is not guaranteed that the other property it needs
% has been set yet. This might result in run-time errors or inconsistent
% values.


% 1. 成员=============================================================
  
% 1.1 公共属性
% GetAccess='public',SetAccess='public'
properties
    dLstd;
    d8Date;
    dNoRows;
    d8MnDate;
    d8MxDate;
    dMnDate;
    dMxDate;
end

% 2. 属性的Set和get方法===============================================

% 2.1 公共Set和get方法
% Access='public',Hidden=false,Sealed=false,Static=false
methods
function set.dLstd(oObj,tValue)
    oObj.dLstd = tValue;
    oObj.dNoRows = size(tValue,1);
    if(oObj.dNoRows==0)
        oObj.dMnDate = [];
        oObj.dMxDate = [];
        return;
    end
    oObj.dMnDate = tValue(1);
    oObj.dMxDate = tValue(end);
end
function set.d8Date(oObj,tValue)
    oObj.d8Date = tValue;
    if(oObj.dNoRows==0)
        oObj.d8MnDate = [];
        oObj.d8MxDate = [];
        return;
    end
    oObj.d8MnDate = tValue(1);
    oObj.d8MxDate = tValue(end);
end
end

% 3. 普通方法==========================================================
    
% 4. 构造方法@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

% 4.1 公共构造方法
% Access='public',Hidden=false,Sealed=false,Static=false
methods
function oObj = HLstd1(varargin)
    if(nargin==0), return; end
    switch class(varargin{1})
    case 'double'
        switch (nargin)
        case 2 % dLstd,d8Date。
            oObj.dLstd = varargin{1};
            oObj.d8Date = varargin{2};
        otherwise,
        end
    otherwise,
    end
end
end
    
end

classdef (Sealed=true) HLstd2 < handle % Hidden=false
% @HLstd2 日期标杆。
%
% 使用说明:
% oObj = HLstd2();
% oObj = HLstd2(dLstd,d8Date);
%
% Problem for case 2:
% I hope that the properties ('dNoRows' etc.) can be calculated
% automatically, when 'dLstd' is set. I use event 'PostSet' as coded
% in this .m file. When I call
%{
    oObj = HLstd2(1835,20080317);
    save('fTest.Mat','oObj','-V7.3');
    oTmp = load('fTest.Mat');
%}
% the object oTmp.oObj has not 'addlistener' automatically. In case,
% 'ConstructOnLoad = true', I got the same result. In a similar code,
% the coming warning occurs:
% Warning:
% handle.listener object could not be loaded because of error:
% handle.listener constructor requires a name.


% 1. 成员=============================================================
  
% 1.1 公共属性
% GetAccess='public',SetAccess='public'
properties (SetObservable=true)
    dLstd;
    d8Date;
end
properties (GetAccess='public',SetAccess='private')
    dNoRows;
    d8MnDate;
    d8MxDate;
    dMnDate;
    dMxDate;
end

% 2. 属性事件的回调函数===============================================

methods (Static)
function HdlPropEvnts(oSrc,oEvnt)
% 要求oObj.eFSort已经赋值。
   oObj = oEvnt.AffectedObject;
   switch oSrc.Name
   case 'dLstd'
       tValue = oObj.dLstd;
       oObj.dNoRows = size(tValue,1);
       if(oObj.dNoRows==0)
           oObj.dMnDate = [];
           oObj.dMxDate = [];
           return;
       end
       oObj.dMnDate = tValue(1);
       oObj.dMxDate = tValue(end);
   case 'd8Date'
       tValue = oObj.d8Date;
       if(oObj.dNoRows==0)
           oObj.d8MnDate = [];
           oObj.d8MxDate = [];
           return;
       end
       oObj.d8MnDate = tValue(1);
       oObj.d8MxDate = tValue(end);
   otherwise,
   end
end
end

% 3. 普通方法==========================================================
    
% 4. 构造方法@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

% 4.1 公共构造方法
% Access='public',Hidden=false,Sealed=false,Static=false
methods
function oObj = HLstd2(varargin)
% 注册事件。
    addlistener(oObj,'dLstd','PostSet',@oObj.HdlPropEvnts);
    addlistener(oObj,'d8Date','PostSet',@oObj.HdlPropEvnts);
    if(nargin==0), return; end
% 构造。
    switch class(varargin{1})
    case 'double'
        switch (nargin)
        case 2 % dLstd,d8Date。
            oObj.dLstd = varargin{1};
            oObj.d8Date = varargin{2};
        otherwise,
        end
    otherwise,
    end
end
end
    
end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us