Object oriented programming with Matlab - Matlab OOP 1 | applied electronics engineering

Latest

Object oriented programming with Matlab - Matlab OOP 1

By Applied Electronics - Monday, February 6, 2017 No Comments
You might be familiar with Matlab procedural programming but how much do you know about Object oriented programming with Matlab? Object oriented programming is a powerful technique of programming for engineers in all field of engineering.

Here we explain a little about object oriented programming and its similarity with other OOP programming language. Then we show you an example with figure object in Matlab. Finally we explain how to access and modify the properties of a figure handle using the get() and set() functions.

Object oriented programming with Matlab



Most commonly we use procedural programming in Matlab which means we create program with data and functions and the program statements are executed one after the other until it reaches the end. However, there is another paradigm of programming which is called Object Oriented Programming(OOP) where classes of data and functions are declared and objects(instances) are created that uses specific classes data and functions. Here we show how to do this in Matlab.

Object Oriented Programming with Matlab is same as OOP with C++. If you know OOP in C++ then terms such as properties, data, methods(functions), classes, object, instances, inheritance, base, parent, superclass, derived, child,  subclass should be familiar. The same terms are used in C++.

We can divide the types of classes as user defined classes Matlab defined classes(built-in classes). That is you can create your own classes and you can use existing Matlab classes.

As an example of Matlab built-in classes are the handle class which is used to create graphical objects for graphics. The figures that you use in Matlab are objects derived from superclass handle.

To be more specific, Matlab organizes internally classes and their objects in a hierarchical structure.
A figure in Matlab follows such hierarchical structure. Matlab sees the computer screen as one object called the root object of the highest parent class. A figure or plot is then subclass of this parent class. The figure or plot is parent to object called axes. The axes object class has further graphics primitives such as line or text and others.

When you type figure in Matlab, Matlab creates a figure object with some default properties.

>> figure

The figure object are individual window instances or object.

To see the properties of the figure object, type

>> f = figure

And you will see its default properties. See how to do this in Matlab,







 What is handle?

In the above statement, f = figure;, f is called handle. It is a pointer to the object figure.

How to access handle properties? get() and set()

 Once a handle is created we can access or modify its properties using get() and set() functions respectively.

To get properties of a handle like f, use the get() function like-

>> get(f)

This will display all the properties of the handle f.

To use any of the properties use the dot operator as in ordinary OOP language. For example to refer to the color property type,

>> ff = get(f);
>> ff.color
 ans =

    0.9400    0.9400    0.9400

The ff created is a structure which has stored in it the properties. Another way to access the properties is by specifying directly the property.

>> get(f, 'Color')
ans =

    0.9400    0.9400    0.9400

See the picture below.






This was about getting information on handle. On the other hand if you want to change the property of a handle set() function is used.



So this is a short introduction of object oriented programming with Matlab. Here we explained a little about object oriented programming and its similarity with other OOP programming language. Then we showed you an example with figure object in Matlab. Finally we explained how to access and modify the properties of a handle.

People around the world uses develop scientific and engineering applications Using MATLAB and as such learning OOP with Matlab is vital.


No Comment to " Object oriented programming with Matlab - Matlab OOP 1 "