Want to use classes and objects in matlab. This is a really quick starter guide. Your class goes in a separate .m file. Let’s say you need to create a monkey class with properties - x location, y location, and the state of the monkey. Here goes monkey.m

classdef monkey
    % monkey -- class definition file

    properties
        x       % monkey x location
        y       % monkey y location
        high    % logical state for monkey
    end    % end properties

    methods
        function m = monkey (x,y,high)
            m.x = x;
            m.y = y;
            m.high = high;
        end    % end function

        function disp(m)
            if m.high
                fprintf('Monkey is at %f %f, and high!\n',m.x,m.y)
            else
                fprintf('Monkey is at %f %f, and low!\n',m.x,m.y)
            end
        end    % end function

    end    % end methods

end    % end class def

Now in the same folder you can access the class. Creating an object:

>> m = monkey(1,1,true);
>> disp(m)
Monkey is at 1.000000 1.000000, and high!

If you want the monkey to be low:

>> m = monkey(1,1,false);
>> disp(m)
Monkey is at 1.000000 1.000000, and low!

Hope this will get you started. You can take a look at this too: http://faculty.ce.berkeley.edu/sanjay/e7/oop.pdf

Last modified: 2017-12-31