Matlab cannot use point operator on object

Today I ran into some strange problem practicing using classes in MATLAB. It seems that MATLAB cannot parse parentheses around the object.

I created a custom class called vector, which has various attributes: magnitude, angle, length in the x and y directions. I overloaded the unary minus operator so that I can have

a = vector(5,50) % creates a vector with magnitude 5 and angle 50 (in degrees)
a.ang % prints the angle
b = -a
b.ang % 230 degrees

Everything is good and good, but I will say that I want to find the angle -a in one line. You expect something like

(-a).ang

but instead i get

 (-a).ang
     |
 Error: Unexpected MATLAB operator.

I can not use

 -a.ang

or because the point operator has a higher priority than minus. Any explanation on why Matlab cannot parse parentheses around an object?

EDIT: Here I created a vector class.

classdef vector
    properties
        mag
        ang % in degrees
        x
        y
    end

    methods
        function v = vector(mag,ang)
            if nargin == 2
                v.mag = mag;
                v.ang = ang;
                v.x = mag*cosd(ang);
                v.y = mag*sind(ang);
            end
        end
        function res = plus(u,v)
            x = u.x + v.x;
            y = u.y + v.y;
            res = vector(norm([x,y]), atan2d(y,x));
        end
        function res = minus(u,v)
            x = u.x - v.x;
            y = u.y - v.y;
            res = vector(norm([x,y]), atan2d(y,x));
        end
        function res = uminus(v)
            res = vector;
            res.x = -v.x;
            res.y = -v.y;
            res.mag = v.mag;
            res.ang = mod(v.ang+180,360);
        end

    end
end
+4
2

, . , Matlab , , , .

MATLAB , . - f (4) (1) - . , f (4) , 1 , f , 4 , , ? , . , .

: https://www.mathworks.com/matlabcentral/newsreader/view_thread/280225

, , , , :

  • SUBSREF
  • .

.

MATLAB, , ?

, , !

+2

, ,

a.ang=[2,4,6,8]

-a.ang

(-a).ang

+1

Source: https://habr.com/ru/post/1627246/


All Articles