MATLAB: Cannot accèss methods of an Object after modified

classmatlab objectsmethods

I have following codes as junk.m & junk2.m
classdef junk
properties
myValue = [];
end
methods
function obj = junk() % constructor
obj.myValue = 11;
end
function obj = me()
end
end
end
function junk2()
obj = junk(); % construct an object of myClass
obj.myValue = 2; % set some fancy values to the object
obj2 = myFunction(obj); % pass the object to a nice function
disp(obj2.myValue) % verify that myValue has changed
end
function obj = myFunction(obj3)
obj.myValue = obj3.myValue + 1;
end
When I call this function I get following output for methods
methods(obj2)
Methods for class struct:
amd ctranspose fieldnames ichol linsolve permute struct2cell
cholinc display fields ilu luinc reshape transpose
methods(obj)
Methods for class junk:
junk me
How can I get both junk $ me methods after I call myFunction(Obj) method?

Best Answer

You need to use handle object. Use the following line in your class definition
classdef junk < handle
You can then write your function as
function myFunction(obj)
obj.myValue = obj.myValue+1;
end
Related Question