How do I use the SUBSREF function to behave as the BUILTIN only for a subset of subscripted references in MATLAB 7.8(R2009a)?
    4 views (last 30 days)
  
       Show older comments
    
I would like to create a SUBSREF function for my class. I am coding the SUBSREF method to behave like a custom SUBSREF for a subset of the subscription symbols and behave like the builtin SUBSREF for the rest. This presents a challenge, as the private and protected properties and methods are accessible inside the custom SUBSREF function. What steps can I take to make sure my private and protected properties and methods are not visible?
Reproduction Steps:
classdef testclass
      properties (Access = protected)
          prop1 = 1;
          prop2 = 2;
      end
      % constructor
      methods (Access = public)
          function TC = testclass()
          end
      end
      % Overloaded subsref
      methods
          function vals = subsref(TC,s)
              switch s(1).type
                  case '.'
                      vals = builtin('subsref',TC,s);
                  case '()'
              end
          end
      end
      methods (Access = protected)
          function showvals(TC)
              disp('prop1 =');
              disp(TC.prop1);
              disp('prop2 =');
              disp(TC.prop2);
          end
      end
end
t = testclass
t.prop1
Accepted Answer
  MathWorks Support Team
    
 on 27 Jun 2009
        This can be acheived by checking for the access information for the Properties and having an if - else statement so that SUBSREF behaves as intended.
An example of the check that you can perform is as follows:
if any(strcmp(s(1).subs,properties(TC))) || any(strcmp(s(1).subs,methods(TC)))
vals = builtin('subsref',TC,s);  
else
error(['''%s'' is not a public property or method of the ''testclass'' class.'],s(1).subs);
end
Another way to get information about the properties of the class is to use meta.class option.
mc = metaclass(TC);
p = [mc.Properties{:}];
   names = {p.Name};
p(strcmp('prop1, names)).GetAccess
0 Comments
More Answers (0)
See Also
Categories
				Find more on Customize Object Indexing in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!