Set object property in object method
1 次查看(过去 30 天)
显示 更早的评论
I am looking for a way to set the value of a property in a class from withtin a method in that same class, but I am unable to find out how to do it. I have made a very simple example of what I want to do in the function updateObj below:
classdef myClass
properties
x
y
end
methods
function obj = myClass(xInit, yInit)
obj.x = xInit;
obj.y = yInit;
end
function updateObj(obj)
if obj.x==7
obj.y = 4;
end
end
end
end
I would like to be able to call the updateObj function using
myObj = myClass(0,0);
myObj.x = 7;
myObj.updateObj;
and then I want myObj.y to return 4, but it will return 0. I understand the reason for this, but I do not know how to change my updateObj function to get the desired result.
1 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Methods 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!