Is MATLAB a good programming language

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 class exhibit reference behavior. Modifying a property value on an instance of a value classes (classes not derived from ), changes the value only within the context in which the modification is made.

The sections that follow describe this behavior in more detail.

Objects Passed 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 Comparison of 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, :

Create an instance of , assigning a value of to its property:

Pass the object to the function , which assigns to the property:

The function modifies its copy of the input object and returns that copy, but does not change the original object.

If the function did not return a value, the modification of the object property would have occurred only on the copy of 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:

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 that you modify the class definition to make a class derived from the class:

Create an instance of , assigning a value of to its property:

Pass the object to the function , which assigns to the property:

The function sets the property of the object referred to by both the returned handle and the original handle:

The variables and refer to the same object:

The function modified the object referred to by the input argument () and returned a handle to that object in .

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 :

Pass a handle object to :

The function overwrites the handle passed in as an argument, but does not overwrite the object referred to by the handle. The original handle still references the original object.

classdef SimpleClass properties Color endmethodsfunction obj = SimpleClass(c) if nargin > 0 obj.Color = c; endendendend
obj = SimpleClass('red');
function y = g(x) x.Color = 'blue'; y = x; end
classdef SimpleHandleClass < handle properties Color endmethodsfunction obj = SimpleHandleClass(c) if nargin > 0 obj.Color = c; endendendend
obj = SimpleHandleClass('red');
y.Color = 'yellow'; obj.Color
function y = g2(x) x = SimpleHandleClass('green'); y = x; end
obj = SimpleHandleClass('red'); y = g2(obj); y.Color