how to do object oriented programming in matlab

MATLAB provides support for Object-Oriented Programming (OOP) and it can be implemented using classes, objects, constructors, methods and properties.

Here is a simple example:

main.m
classdef ClassName
   properties
      PropName
   end
   methods
      function obj = ClassName(inputArg1,inputArg2)
         if nargin > 0
            obj.PropName = inputArg1 + inputArg2;
         end
      end
      function outputArg = exampleMethod(obj,inputArg)
         outputArg = obj.PropName + inputArg;
      end
   end
end
336 chars
16 lines

This is a class named ClassName with a property named PropName and two methods named exampleMethod and ClassName constructor.

To create an object of this class, you can call the constructor with appropriate arguments:

main.m
objectName = ClassName(inputArg1,inputArg2)
44 chars
2 lines

And to call the methods of the class:

main.m
output = objectName.exampleMethod(inputArg)
44 chars
2 lines

You can also define inheritance between classes using inferitance keyword.

related categories

gistlibby LogSnag