Save changes to class properties with dot notation

조회 수: 3 (최근 30일)
Luca Amerio
Luca Amerio 2016년 9월 15일
댓글: Luca Amerio 2016년 9월 15일
Let's say I have a simple class like this one:
classdef MyData
properties
Data = 0;
end
methods
function obj=addData(obj,val)
obj.Data = obj.Data + val;
disp(obj.Data)
end
end
end
If I call twice
a=addData(a,1);
the output is
>> a=addData(a,1);
1
>> a=addData(a,1);
2
however if I use the dot notation the behavior changes like this
>> a.addData(1)
1
>> a.addData(1)
1
The "Data" properties is not updated after the call. Is it possible to use the dot notation and store the value in the object?
Thank you very much

채택된 답변

per isakson
per isakson 2016년 9월 15일
편집: per isakson 2016년 9월 15일
MyData is a value class. Try
a = a.addData(1)
and
classdef MyData < handle
  댓글 수: 1
Luca Amerio
Luca Amerio 2016년 9월 15일
The "< handle" solution is the one I was looking for. Thank you very much

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by