MATLAB Handle Class vs Value Class Differences

1. Copy Behavior

  • Value Class:
    Assigning a value object to a new variable creates an independent copy. Modifying the copy does not affect the original.

    matlab
    Copy
    obj1 = MyValueClass();
    obj2 = obj1;   % Independent copy of obj1
    obj2.Data = 5;  % obj1.Data remains unchanged
  • Handle Class:
    Assigning a handle object copies the reference, not the data. Both variables point to the same object.

    matlab
    Copy
    obj1 = MyHandleClass();
    obj2 = obj1;   % Both reference the same object
    obj2.Data = 5;  % obj1.Data is also updated to 5

2. Memory and Performance

  • Value Class:
    May incur memory overhead when copying large objects repeatedly.
  • Handle Class:
    Efficient for large data, as multiple references share the same instance without duplication.

3. Function Argument Handling

  • Value Class:
    Passed by value. Modifications inside a function do not affect the original unless explicitly returned and reassigned.

    matlab
    Copy
    function modifyValue(obj)
        obj.Data = 10;  % Modifies a local copy
    end
  • Handle Class:
    Passed by reference. Changes in a function directly affect the original object.

    matlab
    Copy
    function modifyHandle(obj)
        obj.Data = 10;  % Original object is modified
    end

4. Methods and Features

  • Handle Class:
    • Inherits from handle, enabling methods like addlistenernotify, and delete (destructor).
    • Supports events and listeners for reactive programming.
    • Destructor (delete) is called automatically when the object is destroyed.
    matlab
    Copy
    classdef Sensor < handle
        events
            DataUpdated
        end
        methods
            function delete(obj)
                disp('Sensor object deleted');
            end
        end
    end
  • Value Class:
    No built-in support for events or destructors. Copying is explicit.

5. Equality Checks

  • Value Class:
    == compares data equality (if the eq method is defined).

    matlab
    Copy
    a = MyValueClass();
    b = a;
    a == b  % True (same data)
  • Handle Class:
    == checks reference identity (whether two variables point to the same object).

    matlab
    Copy
    a = MyHandleClass();
    b = a;
    c = MyHandleClass();
    a == b  % True (same reference)
    a == c  % False (different references)

6. Use Cases

  • Value Class:
    • Immutable data (e.g., mathematical vectors).
    • When independent copies are needed (e.g., configuration settings).
  • Handle Class:
    • Shared resources (e.g., GUI components, file I/O).
    • Large datasets to avoid memory duplication.
    • Observer patterns (using events/listeners).

Example Code Snippets

  • Value Class:
    matlab
    Copy
    classdef Point2D
        properties
            X
            Y
        end
    end
  • Handle Class:
    matlab
    Copy
    classdef Logger < handle
        properties (Access = private)
            LogData
        end
        methods
            function log(obj, message)
                obj.LogData{end+1} = message;
            end
        end
    end

Summary Table

Feature Value Class Handle Class
Copy Behavior Independent copies Shared reference
Memory Efficiency Higher overhead with copies Efficient for shared data
Function Arguments Requires return/reassign Directly modifies original
Events/Destructors Not supported Supported (addlistenerdelete)
Equality Check Compares data Compares references

Use handle classes for shared state or large data, and value classes for independent, immutable instances.