Main Content

Working with Objects in MATLAB

Some MATLAB® functions return objects. Objects combine data (properties) with functions and methods. Object properties contain data, including simple types like numbers or text, or other objects. The functions and methods perform actions on the objects themselves. These functions can act on the object properties or change the state of the object, for example.

Create an Object

When creating an object, you can assign a variable to that object. The variable provides access to the properties and methods of the object. For example, this syntax for the histogram function not only displays a histogram of the data in x but also returns the object as the output h.

h = histogram(x)

Create a histogram object that displays 1000 random numbers. Calling histogram with an output argument displays the graph, the type or class of the object (Histogram), and a partial list of the object properties and their values.

x = randn(1000,1);
h = histogram(x)

Figure contains an axes object. The axes object contains an object of type histogram.

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 16 6 6 4 1 2]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

In the workspace, the histogram object is listed with the other active variables, including the dimensions and the type of object.

object_in_Workspace.jpg

Get and Set Object Properties

Object properties contain data. By changing property values, you can modify certain aspects of an object. You can use dot notation with the variable assigned to the object to access and change its properties.

In the case of a histogram object, properties contain the raw data, the number of bins, the height of each bar, and other information that controls the appearance of the histogram. For example, Orientation is a property of histogram objects that determines whether the bars are displayed horizontally or vertically. You can access the value of the property by entering the name of the object (h), a dot, and the property name.

h.Orientation
ans = 
'vertical'

You can change the value of the property using the same syntax, but with the addition of an equal sign and the new value.

h.Orientation = "horizontal"

Figure contains an axes object. The axes object contains an object of type histogram.

h = 
  Histogram with properties:

             Data: [1000x1 double]
           Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 16 6 6 4 1 2]
          NumBins: 23
         BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000]
         BinWidth: 0.3000
        BinLimits: [-3.3000 3.6000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

Not all object properties are writeable. A property can be read-only, meaning that it can be read, but trying to assign a new value to it returns an error. For example, the Values property of a histogram stores the height of each bar and is calculated when the object is created. It is a read-only property, so you cannot change Values directly.

Functions That Accept Objects

Some functions are designed to perform actions on an object. These functions can be methods, which are defined specifically for one class of objects, or can be functions that accept that object as an ordinary input argument. In either case, you can use the variable for the object as an input. For example, Histogram objects include the functions morebins and fewerbins, which increase and decrease the number of histogram bins, respectively. Access the NumBins property to see how many bins are currently in the histogram.

h.NumBins
ans = 
23

Call morebins to increase the number of bins in histogram h. The morebins function increases the number of bins by approximately 10%, so the number of bins increases from 23 to 26.

morebins(h)

Figure contains an axes object. The axes object contains an object of type histogram.

ans = 
26

Other object-oriented programming languages frequently use dot notation to call methods, such as h.morebins. That syntax also is supported for methods in MATLAB. However, for consistency in sample code, the documentation uses function form for most methods and functions that accept object inputs.

The morebins function does not have any additional input arguments, but that is not true of all functions that accept objects. Besides the object itself, object functions can have additional input arguments, which you can pass to the function with standard function syntax. For example, you can call to an object function that takes two input arguments with this syntax:

functionName(objectVariable,arg1,arg2)

Define Your Own Class-Based Objects

In addition to the objects provided in the MATLAB language, you can define your own class-based objects using object-oriented programming techniques. The language follows standard OO conventions. For more information, start with Creating a Simple Class.

See Also

|