What is handle in Matlab with plot example - Matlab OOP 4 | applied electronics engineering


What is handle in Matlab with plot example - Matlab OOP 4

By Applied Electronics - Monday, February 6, 2017 No Comments
handles in Matlab are instances of objects. For example figure is an example of object. Similarly plot is also an object. In the previous blog post we showed how handles of figure object is created and how figure object properties and methods can be accessed and modified. See What are handles in Matlab and why they are used?

Here we show how plot object handle can be created and how to view and change its properties.

The following statement creates a plot object and its handle hplot.

>> x = 0:0.1:2*pi;
>> y = sin(x);
>> h = plot(x,y)

h = 

  Line with properties:

              Color: [0 0.4470 0.7410]
          LineStyle: '-'
          LineWidth: 0.5000
             Marker: 'none'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [1x63 double]
              YData: [1x63 double]
              ZData: [1x0 double]

  Show all properties

Since we did not suppress the last statement the default properties of the object plot handle are shown.

But we could have suppressed the last statement and accessed its all properties using the properties() function in the following manner.

>> x = 0:0.1:2*pi;
>> y = sin(x);
>> h = plot(x,y);
>> properties(h)

Properties for class matlab.graphics.chart.primitive.Line:


Not all properties are shown here.

Of-course both of the program statements gives the sine wave graph.

What is handle in Matlab with plot example

Similarly you can view the supported methods of the plot object handle using method() function as follows.

>> methods(h)

Methods for class matlab.graphics.chart.primitive.Line:

Line         display      java         
addlistener  double       ne           
addprop      eq           reset        
cat          findobj      set          
clo          get          vertcat      
details      horzcat      
disp         isprop       

Static methods:


Methods of matlab.graphics.chart.primitive.Line inherited from handle.

Once you know the properties and methods you can use them using either get() and set() or using the dot notation as this is object oriented programming.

So for example if you want to get the Color property of the plot handle you would issue,

>> get(h, 'Color')

ans =

         0    0.4470    0.7410

If you wanted to set it diffent color you would issue a set command as follows,

>> set(h, 'Color', [1 0 0])

This just changes the line color to red:

What is handle in Matlab with plot example

Similarly you could use the dot notation to access properties or change them. In the following we show how to access line width information and how to change it using the dot notation.

>> h.LineWidth

ans =


>> h.LineWidth = 2;

What is handle in Matlab with plot example

So in this way you can create plot object handle and change its properties or methods. For introduction to matlab oriented programming see Object oriented programming with Matlab - Introduction.

No Comment to " What is handle in Matlab with plot example - Matlab OOP 4 "