Accessing Object Data

Listing Public Properties

    Note:   Always use the correct case when referring to properties by name.

Display the names of all public object properties using the properties function with the object's class name or with an actual object. For example:

>> properties('MException')

Properties for class MException:
    identifier
    message
    cause
    stack

Getting Property Values

After creating an object, you can access the values of its properties:

try
   a = rand(4);
   a(17) = 7;
catch me % catch creates an MException object named me
   disp(['Current error identifier: ' me.identifier])
end
Current error identifier: MATLAB:indexed_matrix_cannot_be_resized

Access the data in properties using dot notation:

object.PropertyName

For example, you can access the message property of the MException object, me, with this syntax:

me.message
ans =
 In an assignment  A(I) = B, a matrix A cannot be resized.

See Capture Information About Exceptions for more information on using MException objects.

Setting Property Values

Objects often restrict what values you can assign to them. For example, the following timeseries object has 10 data values, each corresponding to a sample time:

tsobj = timeseries(rand(10,1),1:10,'Name','Random Sample');

Now suppose you attempt to set the Data property to a three-element vector:

tsobj.Data = [1 2 3];
Error using timeseries.utreshape (line 864)
Size of the data array is incompatible with the time vector.
...

The timeseries class design ensures that the number of data samples matches the number of time samples. This illustrates one of the advantages a specialized object has over a general purpose-data structure like a MATLAB® struct.

Was this topic helpful?