0

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.

Community
  • 1
  • 1
codeaviator
  • 2,545
  • 17
  • 42
  • 2
    Just curious, why not to have used directly [C# properties](https://msdn.microsoft.com/fr-fr/library/w86s7x04.aspx) (which you can also mimic with [matlab classes](http://fr.mathworks.com/help/matlab/matlab_oop/specifying-properties.html) btw). – CitizenInsane Feb 08 '16 at 15:35
  • 1
    Thanks for your inputs. I didn't know you could call `set` and `get` like that on C#. Regarding MATLAB classes, you were probably referring to this: `function value = get.PropertyName(obj)` right? – codeaviator Feb 08 '16 at 16:21

2 Answers2

4

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 < handle

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

    % Back-store as in C#
    properties(SetAccess=private, GetAccess=private)
        x;
        y;
    end

    % Setters and getters using dependent properties
    properties(Dependent)
        X;
        Y;
    end

    % Definition for setters and getters
    methods
        function [value] = get.X(this)
            value = this.x;
        end
        function [] = set.X(this, value)
            if (value < 0.0), error('X must be positive'); end
            this.x = value;
        end

        ... Same for 'Y' ...

    end

end

For further check for similarities and differences in object oriented programming concepts and implementations between Matlab and C# I recommand your to read documentation on Mathworks website.

CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
0

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;
Arzeik
  • 89
  • 5
  • Thanks for your contribution. However, I am not sure if your example is a class at all. – codeaviator Feb 10 '16 at 15:13
  • You're welcome. Well, of course it is not a class as a standalone piece of code. It should go inside the methods part of your class, but as I saw you already had the structure of the class I saw no point in copying it. It looks like I explained myself poorly. – Arzeik Feb 10 '16 at 16:38
  • I was also considering that option, but I needed to confirm. Thanks again. – codeaviator Feb 10 '16 at 17:39