MATLAB: [basic class syntax question] how can i fix this code

classMATLAB

hello,
now i am studying matlab class code
but i have some problem in my code ,also i can not find which points make errors.
there two classes(example1,example2) that have some methods(example1 is construct instance of example2 and call methods from example1)
classdef example1
properties
value
value1
value2
results
end
methods
function obj = example1(value1,value2)
obj.value1 = example2(value1)%construct other class

obj.value2 = example2(value2)%construct other class
end
function add_2(obj, somevalue)% to activate other class method
example2.add_3(obj.value1,somevalue) % run the other class methods
end
end
end
and other class
classdef example2
properties
value
limit
end
methods
function obj = example2(limitvalue) % definition of the secound class
obj.limit = limitvalue
obj.value = 0
end
end
methods (Static) % when i deleted the '(static)' it makes a error, i don't understand why this error apper??
function add_3(obj, addvalue)
obj.value = obj.value + addvalue
if obj.value > obj.limit
%disp(['add max',str(obj)])
disp('over')
end
end
end
end
first i assign the first class
test1 = example1(10,10) %first i assign the first class
test1.add_2(3) % assign the secound class value as 3
test1.add_2(3) % assign the updated value to secound class value
% i want to assign 3+3 value in secound property test1.value1.value but still "3"
% how can i fix??

Best Answer

The add_3 method of the example2 class uses state information from an instance of the class that was passed into it. While Static methods can be written to accept instances of the class as inputs, most of the time Static methods are intended for operations that do not require instances of the class. As a silly example, a method isMammal of the human class would return true because all humans are mammals.
So make the add_3 method in example2 non-Static, just eliminate that attribute altogether. Once you've done this you can't call it as example2.add_3 (that's the syntax for calling a Static method) so call it as add_3(obj.value1, somevalue).
Now on to the next part of your question:
test1 = example1(10,10) %first i assign the first class
test1.add_2(3) % assign the secound class value as 3
test1.add_2(3) % assign the updated value to secound class value
% i want to assign 3+3 value in secound property test1.value1.value but still "3"
% how can i fix??
Your classes do not subclass from the handle class so they are value classes. What does this mean? This documentation page goes into details, but basically what it means is that when you make a copy of a value class they are independent, whereas if you make a copy of a handle class they refer to the same thing. For instance, a regular numeric array behaves as a value:
A = 1:10;
B = A;
B(5) = 42;
A(5) % still 5, unchanged by the change to B(5)
A figure handle is a handle object.
f1 = figure;
f1.Color = 'g'; % Figure is green
f2 = f1;
f2.Color = 'r'; % Figure is red
f1.Color % 'r' because the figure on screen to which f1 and f2 both refer is red.
So when you run the command:
test1.add_2(3)
you're making a copy of test1 inside add_2, modifying that copy of test1 (which doesn't affect test1), then returning the copy and effectively discarding it. What can you do to make the changes "stick"? The two main options are to assign the output from add_2 (which is a copy of test1) back to test1 itself, overwriting the original with the modified copy. This will also require some changes to add_3 to also return a modified object.
test1 = test1.add_2(3);
Another would be to make your example1 class a handle class, like a figure handle. [That documentation page to which I linked tells you how to make a class a handle class.] Then your command test1.add_2(3) would pass the handle test1 into add_2, you would modify the handle inside add_2, and the handle test1 in the workspace where add_2 was called would reflect those changes.