Code covered by the BSD License  

Highlights from
On-The-Fly Definition of Custom Matrix Objects

On-The-Fly Definition of Custom Matrix Objects

by

 

07 Feb 2010 (Updated )

Class of matrix-like objects with on-the-fly definable methods.

DataObj
classdef DataObj < MatrixObj
%DataObj - a subclass of MatrixObj
%
% See UserGuide for full details and examples.
%
% Sometimes it is desirable to customize or modify the behavior of other 
% existing data types or classes. The DataObj subclass is tailored to situations 
% like these. Instead of a "Params" property, this subclass contains a "Data" 
% property for holding an object data variable. Typically, this would be an existing
% MATLAB numeric variable type, although this is not strictly necessary. When a function 
% Ops.MethodName is called, the function will receive this Data variable as
% input, in place of the actual DataObj object. This is  in contrast to MatrixObj
% Ops functions, which  always receive the entire object as input.
% The result of a DataObj operation is also always a new DataObj
% object, one whose Data property holds the results of the computation. The function Ops.size()
% is an exception to this rule. The size() method will return normal numeric output.
% These  ideas are demonstrated in the following example. 
% 
% EXAMPLE: In this example, DataObj is used to create a specialized 
% array type which invokes bsxfun() for a variety of operations. This can be
% a useful way of circumventing bsxfun's lengthy functional syntax.
% 
% 
%  P=DataObj;
%   P.Data=[1,2;3,4].',
%     
%         P.Ops.minus=@(A,B) bsxfun(@minus,A,B); 
%         P.Ops.plus= @(A,B) bsxfun(@plus,A,B); 
%         P.Ops.times=@(A,B) bsxfun(@times,A,B); 
%         P.Ops.rdivide= @(A,B) bsxfun(@rdivide,A,B);
%         P.Ops.ldivide= @(A,B) bsxfun(@ldivide,A,B);     
%         P.Ops.power=@(A,B) bsxfun(@power,A,B);  
%         P.Ops.eq=@(A,B) bsxfun(@eq,A,B); 
%         P.Ops.ne= @(A,B) bsxfun(@ne,A,B); 
%         P.Ops.lt=@(A,B) bsxfun(@lt,A,B); 
%         P.Ops.le= @(A,B) bsxfun(@le,A,B);
%         P.Ops.gt= @(A,B) bsxfun(@gt,A,B);
%         P.Ops.ge=@(A,B) bsxfun(@ge,A,B); 
%         P.Ops.and= @(A,B) bsxfun(@and,A,B);     
%         P.Ops.or=@(A,B) bsxfun(@or,A,B); 
%  
%         
%   Q=P-[1,2],
%   R=P+[3;7],
%
%         P =
% 
%              1     3
%              2     4
% 
% 
%         Q =
% 
%              0     1
%              1     2
% 
% 
%         R =
% 
%              4     6
%              9    11
% 
%
% Notice that the above P.Ops functions are written in terms of normal MATLAB
% numerical input variables. There is no need for these functions to extract
% the Data to be operated upon from the DataObj. This extraction is done by the
% DataObj class methods automatically.
%
%Copyright 2010-08-22, Matt Jacobson, Xoran Technologies, http://www.xorantech.com 




   %properties (Access=protected, Hidden=false)
    properties (Constant=true, Hidden=true);
     DefaultTemplate=DataObj.MakeDefaultTemplate;
    end
    methods (Access=protected, Hidden=true);
        function Name=TemplateName(obj)
           Name='DefaultTemplate';
        end
    end

  properties
     
      Data;
      
  end
    

   methods 
    
       function obj=DataObj(Data,varargin)
                
           obj=obj@MatrixObj(varargin{:});
 
           
           if nargin>=1 
               obj.Data=Data;
           end
           
      
           
           if length(varargin)<2
               
            tmp=obj.DefaultTemplate;
            
            Ops=struct(tmp{:});        
            obj.Ops=Ops;
            
           end
           
       end
 
       
   end
    
      methods (Static, Hidden, Access=protected)
        
        
        function template=MakeDefaultTemplate
            
            template=MatrixObj.OpsTemplate;
            template(2,:)=cellfun(@(name) str2func(name), template(1,:), 'uni',0);

            [idx,loc]=ismember({'display','subsref','subsasgn','subsindex','end'},...
                                template(1,:)); %some exceptions
                            
            template(2,loc)={[]};
            
        end

    
     end 
    
end




Contact us