prevent handle class from being copied in matlab

You can prevent instances of a handle class from being copied by implementing the matlab.mixin.Copyable class and its copy method in your handle class as follows:

main.m
classdef MyHandleClass < handle & matlab.mixin.Copyable
   % class definition
   methods
      function cpObj = copy(obj)
         % This copy method prevents copying of MyHandleClass objects
         error('Cannot copy object of class MyHandleClass')
      end
   end
end
273 chars
10 lines

This will throw an error if any attempt to copy the object is made.

related categories

gistlibby LogSnag