Modifying Objects
MATLAB classes can define public properties, which you can modify by explicitly assigning values to those properties on a given instance of the class. However, only classes derived from the handle class exhibit reference behavior. Modifying a property value on an instance of a value classes (classes not derived from handle), changes the value only within the context in which the modification is made.
The sections that follow describe this behavior in more detail.
Passing Objects to Functions
MATLAB passes all variables by value. When you pass an object to a function, MATLAB copies the value from the caller into the parameter variable in the called function.
However, MATLAB supports two kinds of classes that behave differently when copied:
Handle classes — a handle class instance variable refers to an object. A copy of a handle class instance variable refers to the same object as the original variable. If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and copied handles.
Value classes — the property data in an instance of a value class are independent of the property data in copies of that instance (although, a value class property could contain a handle). A function can modify a value object that is passed as an input argument, but this modification does not affect the original object.
See Comparing Handle and Value Classes for more information on the behavior and use of both kinds of classes.
Passing Value Objects.When you pass a value object to a function, the function creates a local copy of the argument variable. The function can modify only the copy. If you want to modify the original object, return the modified object and assign it to the original variable name. For example, consider the value class, SimpleClass :
classdef SimpleClass
properties
Color
end
methods
function obj = SimpleClass(c)
if nargin > 0
obj.Color = c;
end
end
end
end
Create an instance of SimpleClass, assigning a value of red to its Color property:
obj = SimpleClass('red');
Pass the object to the function g, which assigns blue to the Color property:
function y = g(x)
x.Color = 'blue';
y = x;
end
y = g(obj);
The function g modifies its copy of the input object and returns that copy, but does not change the original object.
y.Color
ans =
blue
obj.Color
ans =
red
If the function g did not return a value, the modification of the object Color property would have occurred only on the copy of obj within the function workspace. This copy would have gone out of scope when the function execution ended.
Overwriting the original variable actually replaces it with a new object:
obj = g(obj);
Passing Handle Objects.When you pass a handle to a function, the function makes a copy of the handle variable, just like when passing a value object. However, because a copy of a handle object refers to the same object as the original handle, the function can modify the object without having to return the modified object.
For example, suppose you modify the SimpleClass class definition to make a class derived from the handle class:
classdef SimpleHandleClass < handle
properties
Color
end
methods
function obj = SimpleHandleClass(c)
if nargin > 0
obj.Color = c;
end
end
end
end
Create an instance of SimpleHandleClass, assigning a value of red to its Color property:
obj = SimpleHandleClass('red');
Pass the object to the function g, which assigns blue to the Color property:
y = g(obj);
The function g sets the Color property of the object referred to by both the returned handle and the original handle:
y.Color
ans =
blue
obj.Color
ans =
blue
The variables y and obj refer to the same object:
y.Color = 'yellow';
obj.Color
ans =
yellow
The function g modified the object referred to by the input argument (obj) and returned a handle to that object in y.
MATLAB Passes Handles by Value.A handle variable is a reference to an object. MATLAB passes this reference by value.
Handles do not behave like references in C++. If you pass an object handle to a function and that function assigns a different object to that handle variable, the variable in the caller is not affected. For example, suppose you define a function g2:
function y = g2(x)
x = SimpleHandleClass('green');
y = x;
end
Pass a handle object to g2:
obj = SimpleHandleClass('red');
y = g2(obj);
y.Color
ans =
green
obj.Color
ans =
red
The function overwrites the handle passed in as an argument, but does not overwrite the object referred to by the handle. The original handle obj still references the original object.