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 for class MException:

Getting Property Values

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

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

Access the data in properties using dot notation:


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

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?