Is it possible to temporarily disable the private attribute of object properties in debug mode in MATLAB 7.9 (R2009b)?

10 views (last 30 days)
When I'm stepping through my code, it would be nice to be able to "peek" into the contents of private properties in my objects from scopes where this would not normally be possible during regular program execution. Is there a way to do this without having to set all of my properties to public temporarily and then setting them back to private when I'm done debugging?
For example, if I create the class definition:
classdef test
properties(SetAccess= private, GetAccess=private)
a = 'a';
b='b';
end
properties
c='c';
d='d';
end
end
and instantiate this object in another function, 'call_test.m':
function call_test
t = test;
end
I would like to be able to see the private properties of this object when running 'call_test.m' in debug mode.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
The ability to temporarily disable the private attribute of object properties does not currently exist in MATLAB 7.9 (R2009b). You can access the private properties of objects while debugging using either of the following methods:
1. Temporarily set the GetAccess attribute of the properties to public
2. Create a public method to display the contents of the private properties, as in the following class definition. This method can use Meta-Classes to analyze the class definition and determine all property names.
classdef test
properties(SetAccess= private, GetAccess=private)
a = 'a';
b='b';
end
properties
c='c';
d='d';
end
methods
function mydisp(obj)
% To display the private data
m = metaclass(obj);
metaProps = m.Properties;
nProps = numel(metaProps);
disp('Properties:')
for n=1:nProps
fprintf(' %s: %d (SetAccess = %s; Get Access = %s)\n',...
metaProps{n}.Name,metaProps{n}.DefaultValue,...
metaProps{n}.SetAccess, metaProps{n}.GetAccess)
end
end
end
end
  1 Comment
per isakson
per isakson on 7 Jun 2014
Edited: per isakson on 7 Jun 2014
"temporarily disable the private attribute of object properties" would invite the developer to access private properties in tests, which is disputable. This applies even more to private methods.
However, I think that in debug mode ("K>>") the private attribute should neither stop interactive access to properties nor to methods.

Sign in to comment.

More Answers (1)

per isakson
per isakson on 7 Jun 2014
Edited: per isakson on 30 Sep 2017
The answer by the Team does not include the current values of the private properties. I modified their class to display the current values.
>> dpp = display_private_property_demo;
>> dpp = dpp.setPrivate( 'a', 'new_a' );
>> dpp = dpp.setPrivate( 'b', 'mod_b' );
>> dpp.mydisp
Name Default Current SetAccess GetAccess
a a new_a private private
b b mod_b private private
c c c public public
d d d public public
>>
where
classdef display_private_property_demo
properties ( SetAccess = private, GetAccess = private )
a = 'a';
b = 'b';
end
properties
c = 'c';
d = 'd';
end
methods
function mydisp( this )
% To display the private data
m = metaclass( this );
metaProps = m.Properties;
nProps = numel(metaProps);
fprintf( '%-12s%-12s%-12s%-12s%-12s\n' ...
, 'Name','Default','Current','SetAccess','GetAccess' )
for jj = 1 : nProps
fprintf( '%-12s%-12s%-12s%-12s%-12s\n' ...
, metaProps{jj}.Name ...
, metaProps{jj}.DefaultValue ...
, this.( metaProps{jj}.Name ) ... <<<<<<
, metaProps{jj}.SetAccess ...
, metaProps{jj}.GetAccess ...
)
end
end
function this = setPrivate( this, name, value )
this.( name ) = value;
end
end
end

Categories

Find more on Historical Contests in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!