Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Getting Information About Properties

Information Contained in the meta.property object

The meta.property class is useful for determining the settings of property attributes. The writable properties of a meta.property object correspond to the attributes of the associated property. The values of the writable meta.property properties correspond to the attribute setting specified in the class definition.

For example, create a default containers.Map object and use the handle findprop method to get the meta.property object for the Count property:

mp = findprop(containers.Map,'Count')

mp = 

  meta.property handle
  Package: meta

  Properties:
                   Name: 'Count'
            Description: 'Number of pairs in the collection'
    DetailedDescription: ''
              GetAccess: 'public'
              SetAccess: 'private'
                 Sealed: 0
              Dependent: 1
               Constant: 0
               Abstract: 0
              Transient: 1
                 Hidden: 0
          GetObservable: 0
          SetObservable: 0
               AbortSet: 0
              GetMethod: []
              SetMethod: []
          DefiningClass: [1x1 meta.class]

The preceding meta.property display shows that the default Map object Count property has public GetAccess and private SetAccess, is Dependent, and Transient. See Table of Property Attributes for a list of property attributes.

If you are working with a class that is not a handle class, get the meta.property objects from the meta.class object. All meta-classes are subclasses the handle class. Use the metaclass function if you have an instance or the ? operator with the class name:

mc = ?containers.Map

mc = 

  meta.class handle
  Package: meta

  Properties:
                   Name: 'containers.Map'
            Description: 'MATLAB Map Container'
    DetailedDescription: 'MATLAB Map Container'
                 Hidden: 0
                 Sealed: 0
        ConstructOnLoad: 1
        InferiorClasses: {0x1 cell}
             Properties: {4x1 cell}
                Methods: {33x1 cell}
                 Events: {[1x1 meta.event]}
       EnumeratedValues: {0x1 cell}
           SuperClasses: {[1x1 meta.class]}
      ContainingPackage: [1x1 meta.package]

The meta.class object property named Properties contains a cell array of meta.property objects, one for each property defined by the containers.Map class. For example, the name of the property associated with the meta.property object in cell 1 is:

mc.Properties{1}.Name

ans =

Count

The meta.class object contains a meta.property object for hidden properties too. Compare the result with the properties function, which returns only public properties:

properties('containers.Map')

Properties for class containers.Map:

    Count
    KeyType
    ValueType

The type property is private in this class so the properties function does not list it:

mc.Properties{1}

ans = 

  meta.property handle
  Package: meta

  Properties:
                   Name: 'Count'
            Description: 'Number of pairs in the collection'
    DetailedDescription: ''
              GetAccess: 'public'
              SetAccess: 'private'
                 Sealed: 0
              Dependent: 1
               Constant: 0
               Abstract: 0
              Transient: 1
                 Hidden: 0
          GetObservable: 0
          SetObservable: 0
               AbortSet: 0
              GetMethod: []
              SetMethod: []
          DefiningClass: [1x1 meta.class]

Indexing Meta-Class Objects

You can access other meta-class objects directly from the meta.class object properties. For example, the statement:

mc = ?containers.Map;

returns a meta.class object:

class(mc)

ans =

meta.class

Referencing the Properties meta.class property returns a cell array with one cell for each property of the containers.Map class:

class(mc.Properties)

ans =

cell

Each cell contains a meta.property object:

class(mc.Properties{1})

ans =

meta.property

The Name property of the meta.property object contains a character string that is the name of the property:

class(mc.Properties{1}.Name)

ans =

char

Apply standard MATLAB indexing to access information in meta-classes. The following expression accesses the first meta.property object in the cell array of meta.property objects, in the meta.class Properties property It returns the first (C) and last (t) letters of the string contained in the Name property

mc.Properties{1}.Name([1 end])

ans =

Ct

Example — Finding Properties with Specific Attributes

This example implements a function that finds properties with specific attribute settings. For example, you can find objects that define constant properties (Constant attribute set to true) or determine what properties are read-only (GetAccess = true, SetAccess = false). The findAttrValue function returns a cell array of property names that set the specified attribute.

This function access information from meta-classes using these techniques:

function cl_out = findAttrValue(obj,attrName,varargin)

   % Determine if first input is object or class name
   if ischar(obj)
      mc = meta.class.fromName(obj);
   elseif isobject(obj)
      mc = metaclass(obj);
   end

   % Initialize and preallocate
   ii = 0; numb_props = length(mc.Properties);
   cl_array = cell(1,numb_props);

   % For each property, check the value of the queried attribute
   for  c = 1:numb_props

      % Get a meta.property object from the meta.class object
      mp = mc.Properties{c}; 

      % Determine if the specified attribute is valid on this object
      if isempty (findprop(mp,attrName))
         error('Not a valid attribute name')
      end
      attrValue = mp.(attrName);

      % If the attribute is set or has the specified value,
      % save its name in cell array
      if attrValue
         if islogical(attrValue) || strcmp(varargin{1},attrValue)
            ii = ii + 1;
            cl_array(ii) = {mp.Name}; 
         end
      end
   end
   % Return used portion of array
   if exist('cl_array','var')
      cl_out = cl_array(1:ii);
   else
      cl_out = {[]};
   end
end

Finding Property Attributes

Suppose you have the following containers.Map object:

mapobj = containers.Map({'rose','bicycle'},{'flower','machine'});

Find properties with private SetAccess:

findAttrValue(mapobj,'SetAccess','private')

ans = 

    'Count'    'KeyType'    'ValueType'    'serialization'

Find properties with public GetAccess:

findAttrValue(mapobj,'GetAccess','public')

ans = 

    'Count'    'KeyType'    'ValueType'
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS