Filter löschen
Filter löschen

Save changes to class properties with dot notation

2 Ansichten (letzte 30 Tage)
Luca Amerio
Luca Amerio am 15 Sep. 2016
Kommentiert: Luca Amerio am 15 Sep. 2016
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

Akzeptierte Antwort

per isakson
per isakson am 15 Sep. 2016
Bearbeitet: per isakson am 15 Sep. 2016
MyData is a value class. Try
a = a.addData(1)
and
classdef MyData < handle
  1 Kommentar
Luca Amerio
Luca Amerio am 15 Sep. 2016
The "< handle" solution is the one I was looking for. Thank you very much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by