Cebri February 2016

Adapt C# Set/Get class methods into MATLAB class

Take the following custom C# class as an example, it represents a point:

public class Point
{
    // Attributes:

    double x;
    double y;

    // Constructor:

    public Point(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    // Methods:

    public void SetX(double x)
    {
        this.x = x;
    }

    public void SetY(double y)
    {
        this.y = y;
    }

    public double GetX()
    {
        return this.x;
    }

    public double GetY()
    {
        return this.y;
    }
}

I would like to adapt this same class to MATLAB. My goal is to implement the Set and Get methods and the Constructor, as similar as possible to the C# way.

I am aware that MATLAB does not allow self-reference, as I read on this question. However, I know that I should problably want to start the class with the following header:

classdef Point < handle

I read about this here and here. This statement will declare my custom Point class as the MATLAB handle class, by having it inherit from the handle superclass. I understand that this will allow me to implement the Set/Get functionality that I am trying to emulate, as well as for the Constructor.

This is what I have so far:

classdef Point < handle

    % Attributes
    properties (Access = private)
        x = [];
        y = [];
    end

    % Methods
    methods

        % Constructor: First method, must have the same name as the class.
        function Point(x,y)

        end

        % Set and Get methods:
        function SetX(x)

        end

        ...

    end
end

From this point on, I don't know how to continue.

Answers


Arzeik February 2016

You can do something like:

function obj Point(x,y)
     obj.x=x;
     obj.y=y;
end

function setX(obj,x)
     obj.x=x;
end

function getX(obj)
     return obj.x;
end

And something similar for the rest of the methods.

Then you can use it like this:

p1=Point(5,4);
p1.setX(3);
xp1=p1.getX;


CitizenInsane February 2016

First as describe in comments to your question, lets first simplify your C# using properties:

public class Point
{
    public Point(double x, double y)
    {
         this.X = x;
         this.Y = y;
    }

    public double X { get; set; }
    public double Y { get; set; }
}

Now to create similar class in Matlab you indeed have to create a classdef file and if you want to mimic pass by reference you'll have to derive from handle class else object will be passed by value. So using properties your maltab class will look like:

classdef Point < handle

    methods
        function [this] = Point(x, y)
            this.X = x;
            this.Y = y;
        end
    end

    properties
        X;
        Y;
    end

end

Now suppose you need to implement setters/getters for validation purpose for example. Your C# class will then look like:

public class Point
{
    public Point(double x, double y)
    {
         this.X = x;
         this.Y = y;
    }

    private double x; // Back-store
    public double X // Setters and getters
    { 
         get 
         { 
             return x; 
         } 
         set 
         {
             if (value < 0.0) { throw new InvalidArgumentException("X must be positive"); }
             x = value;
         } 
    }

    ... same for 'Y' ...
}

You can implement similar getter/setter in Matlab using dependent properties concept. Syntax is quite similar to C# one while a little more over-verbose:

classdef Point 

Post Status

Asked in February 2016
Viewed 2,450 times
Voted 9
Answered 2 times

Search




Leave an answer