Set object property in object method

조회 수: 2 (최근 30일)
Niklas Nylén
Niklas Nylén 2011년 12월 7일
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
per isakson
per isakson 2011년 12월 8일
This would have worked for a handle class, i.e. myClass < handle

댓글을 달려면 로그인하십시오.

채택된 답변

Niklas Nylén
Niklas Nylén 2011년 12월 7일
I found a solution to this, change the updateObj function to return obj like this
function obj = updateObj(obj)
...
end
and call it with
myObj = myObj.updateObj;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Propagation and Channel Models에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by