Example of a Method
The addData method adds a value to the Data property of MyData objects. The
mustBeNumeric function restricts the value of the Data property to numeric values. The
property has a default value of 0.
The addData method returns the modified object, which you can reassign to the same
variable.
classdef MyData
properties
Data {mustBeNumeric} = 0
end
methods
function obj = addData(obj,val)
if isnumeric(val)
newData = obj.Data + val;
obj.Data = newData;
end
end
end
end
a = MyData;
a = addData(a,75)
a =
MyData with properties:
Data: 75
Calling Methods
Either of the following statements is correct syntax for calling a method, where obj is an
object of the class defining the methodName method:
obj.methodName(arg)
methodName(obj,arg)
Method Files
You can define methods: