MATLAB: Geting started with oop

beginnerclassoopown classessubclass

hi everybody, i got a probally simple question but would be gratefull if anyone could help
i got the following code (that does not work)
classdef Knot
properties
x
y
end % properties
methods
function K = Knot(x,y)
K.x=x;
K.y=y;
end%functions
end%methods

end %class

classdef Slab<Knot
properties
Material
StartKnot
EndKnoten
end%properties
methods
function S = Slab(StartKnot,EndKnot)
S.StartKnot = StartKnot;
S.EndKnot = EndKnot;
end%function
end%methods
end%class
in my mind the subclass Slab should get the properties x and y.
trying to run it gives me this:
>> k1 = Knoten(1,1);
>> k2 = Knoten(2,2);
>> s= Stab(k1)
??? Input argument "x" is undefined.
Error in ==> Knoten>Knoten.Knoten at 8
K.x=x;
Error in ==> Stab>Stab.Stab at 8
function S = Stab(StartKnoten)
acctually i wanted to get a Slab class that has the properties of Knot and could be acceses like this:
k=Knot(1,1);kk = Knot(1,2);S = Slab(k1,k2);
Slab.StartKnot.x should deliver the x propertie of Knot
obviously i got something wrong
thankfull for help

Best Answer

Firstly, you need to be more careful with the names. Should it be Knot or Knoten and Slab or Stab.
Secondly, the constructor of Knot requires values of the two inputs. Add the line:
if nargin == 0, return, end
to Knot
>> clear all, clear classes
>> k1 = Knot(1,1);
>> k2 = Knot(2,2);
>> s = Slab(k1,k2)
s =
Slab
Properties:
Material: []
StartKnot: [1x1 Knot]
EndKnot: [1x1 Knot]
x: []
y: []
Methods, Superclasses
>> s.StartKnot.x
ans =
1
Thirdly, do you really want Slab both to inherit Knot and have Knot objects as values of properties?
Related Question