简体   繁体   中英

Matlab - OOP Methods

I am working on some OOP in MATLAB. I am currently creating a class as follows:

classdef uavclass
    properties
        %all properties of the uav
        position = [0,0,0]
        charge = 100;
        destination = [0,0,0];
        maxVertClimb = 2;
        maxHorizSpeed = 5;
        sensorRange = 25;
        unloadingTime = 60;
        safeDistance = 5;
        chargingTime = 300;
        minCruiseAlt = 20;
        maxCruiseAlt = 70;
    end
    methods(Static)
        function uav = uavclass(x,y,z)
            uav.position = [x,y,z]
        end
    end
end

I can create a uav at a position by calling:

uav1 = uavclass(20,20,0);
uav2 = uavclass(40,40,0);
uav3 = uavclass(50,50,0);
uav4 = uavclass(80,80,0);
uav5 = uavclass(90,90,0);

Whenever I try to use any kind of method on the uav objects I get an error. An example is simply trying change the sensorRange of some uav

    function changesense(b) 
        uav.sensorRange = b
    end

have also tried uavclass.sensorRange . I try to call this with uav1.changesense(5) , but it does not change the value. I have also tried with and without using (Static) for methods, and changesense(uav1,b) etc.

What am I doing wrong?

You always have to have a first parameter to each method which defines the object which is modified. That means, you need to implement it like this:

function obj = changesense(obj, b) 
    obj.sensorRange = b
end

This is just the concept how it works in MATLAB. Also have a look here for some simple OOP examples: https://de.mathworks.com/help/matlab/matlab_oop/getting-familiar-with-classes.html and this docu about different types of methods: https://de.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html#brqy87j In case you wanna use a static method (not working in the object's context, you would NOT need the first argument).

It is the same concept like self in Python.

And everything you'd want to know about OOP in matlab is fully documented in any sub-page of this main page: https://de.mathworks.com/help/matlab/object-oriented-programming.html

There are several approaches to this problem. The "quick-and-dirty"-est one would be annotating the properties section with the following:

properties (Access = public)

this way you will be able to read and write the the object properties (or " fields ") as if it was a struct .


Several notes:

  1. You can create several methods and properties blocks, each with their own attributes. For example, this way you can some methods blocks to contain Static methods, while other can define private etc. I would suggest separating your properties into two blocks, where the 1 st contains some constants, and the other contains your actual "instance variables":

     properties (Constant = true) %// Class constants MAX_VERT_CLIMB = 2; MAX_HORIZ_SPEED = 5; ... end properties (Access = public, Constant = false) %// Instance variables sensorRange@double scalar = []; position@double vector = []; ... end
  2. Accessor & Mutator methods: as mentioned in Tim's answer one might want to use methods to change and access the values of instance fields, as these generally provide more flexibility in the long run. You can read more about this in eg Bloch's Effective Java " Item 14: In public classes, use accessor methods, not public fields ".

  3. I don't think it's correct to annotate the methods block which contains the constructor with any Method Attributes . I say this only because this is how it appears in MATLAB's examples, I believe this has something to do with subclassing certain "convenience classes" that require the constructor block to have no attributes, but there may very well be another explanation.

I think a simple change in your code should do.

classdef uavclass
    properties
        %all properties of the uav
        position = [0,0,0]
        charge = 100;
        destination = [0,0,0];
        maxVertClimb = 2;
        maxHorizSpeed = 5;
        sensorRange = 25;
        unloadingTime = 60;
        safeDistance = 5;
        chargingTime = 300;
        minCruiseAlt = 20;
        maxCruiseAlt = 70;
    end
    methods
        function uav = uavclass(x,y,z) % this is constructor
            uav.position = [x,y,z];
        end
    end
end

Note that a name of your function is the same as the name of a class. That suggests that this function is a constructor. You should remove the Static attribute in methods . Constructors must also follow a couple of rules. One of them is that a function must return a reference to an object that a constructor creates, in your case it is uav but it could be anything. I usually use obj .

Remember that you have decided to make your properties public. Otherwise you could use one of the attributes: private or protected and control access to properties by setters or getters.

If you define any other method in your class, the first argument must be your a reference to your object. If you make changes to your object, your function needs to return this variable! Matlab makes changes only to a copy of an input object. This object is lost, unless you return it from a function.

classdef uavclass
    % code
    methods
        function obj = uavclass(x,y,z)
            obj.position = [x,y,z];
        end
        function uav = mymethod(uav, arg1, arg2) % uav contains a reference to an instance of an object
            uav.minCruiseAlt = arg1;
            uav.maxCruiseAlt = arg2;
        end
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM