Search code examples
oopmatlabprivateprotected

Inspecting and editing private/protected properties of objects


In MATLAB 2011b, I have some classes that have a number of private or protected class properties. This is by design, as I do not want these to be viewable, readable, writable, etc. by anything other than my class's own getters/setters.

However, there are times during development when I want to see what the state of these properties are. The class object is available to inspect in the workspace, but when I access the Variable Editor tool by double-clicking the object in the Workspace, none of the private/protected properties are visible.

I can understand that this is probably by design, but I'm hoping there is a setting I'm missing that will let me view them.


Solution

  • If you don't want to modify the attributes for debugging, then you can instead use the debugger to inspect the private/protected variables. For convenience, you can even write a method that gets you into the debugger, so that you don't have to manually set a stop:

    classdef testObj_debug
        properties (Access=private)
            p1 = 4;
        end
    
        methods (Hidden)
            function checkProps(obj)
                keyboard
            end
        end
    end
    

    If you call to = testObj_debug, to.checkProps, you are accessing the debug mode, in which the hidden property p1 is accessible and inspectable.