example of object oriented program in matlab

Here's an example of an object-oriented program in MATLAB:

main.m
% Define a class called "Person"
classdef Person
    
    properties
        name
        age
        gender
    end
    
    methods
        % Define a constructor method
        function obj = Person(name, age, gender)
            obj.name = name;
            obj.age = age;
            obj.gender = gender;
        end
        
        % Define a method to print the person's information
        function printInfo(obj)
            fprintf('Name: %s\nAge: %d\nGender: %s\n', obj.name, obj.age, obj.gender);
        end
    end
    
end
539 chars
25 lines

In this example, we've created a class called "Person" with three properties (name, age, and gender) and two methods (a constructor and a print method). The constructor method is called when a new object is created, and sets the object's properties to the specified values. The printInfo method simply prints the person's information to the console.

Here's how we could use this class to create a new person object, set its properties, and print out its information:

main.m
% Create a new person object
p = Person('Alice', 25, 'female');

% Print out the person's information
p.printInfo();
117 chars
6 lines

This would output:

main.m
Name: Alice
Age: 25
Gender: female
35 chars
4 lines

gistlibby LogSnag