Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


Paul
Posts:
17
Registered:
5/18/11


Re: Determining 'classname' such that isa(v,'classname') will be true
Posted:
Apr 20, 2013 7:07 PM


"Steven_Lord" <slord@mathworks.com> wrote in message <kkotjj$pu7$1@newscl01ah.mathworks.com>... > > > "Paul " <paulremovethispartjackson@jhuapl.edu> wrote in message > news:kknj76$2el$1@newscl01ah.mathworks.com... > > "Steven_Lord" <slord@mathworks.com> wrote in message > > <kkmaj0$t60$1@newscl01ah.mathworks.com>... > > *snip* > > > Steve, > > > > I missed in the doc that SuperclassList contains only the classes from > > which the object inherits *directly*. For some reason I thought it would > > return all of the parents. > > That's a reasonable expectation, but it does only look at direct "parents" > of the class. You need to ask the parents for information about the > grandparent classes and so on back through the object's "family tree." > > > I don't think I was trying to do something tricky or clever. I have a > > function that can take either an ss or tf or zpk object as input and > > wanted to do a simple error check, like "if ~isa(in,'numlti')", but > > couldn't find a clear description in the doc for the common class from > > which those three inherit (there is a diagram in the Control System > > Toolbox documentation, but it's not what I would think of as an > > inhertiance tree and doesn't even contain the term 'numlti'). So I > > started exploring the use of class(h) and metaclass to figure it out > > myself. > > If you want to check that the input is either an SS or a TF or a ZPK object, > either: > > if ~(isa(in, 'ss')  isa(in, 'tf')  isa(in, 'zpk')) > > or (if you don't want to accept subclasses of one of those three classes) > > if ~ismember(class(in), {'ss', 'tf', 'zpk'}) > > would work. > > One potential problem with checking for the superclass is that your function > would not only accept SS or TF or ZPK objects but would accept ANY object > that inherits from numlti. For instance, a RandStream object is a handle > object (and subclasses the handle class) but checking: > > isa(inputExpectedToBeARandStream, 'handle') > > is not sufficient to ensure it can behave like a RandStream object. > RandStream objects can do certain things (generate random numbers, for > example) that not all handle classes can do. > > > Maybe a whatis type of function would be useful in general to provide > > insight into a class hierarchy? I don't really use OOP in Matlab, and so > > don't know if there are any good usecases, other than the one I described > > above. > > Hmm ... I just learned something. I did not know of the existence of this > function until I did a search: > > http://www.mathworks.com/help/matlab/ref/superclasses.html > > Note that this is not necessarily going to give you the whole hierarchy; if > there are any Hidden classes they will not be listed. Using this on an ss > object does list lti. [And no, there is no corresponding SUBCLASSES > function. Classes _must_ know about their superclasses since the > specification of the classes from which they inherit are explicitly part of > their class definition file; they don't necessarily know who their > "children" are.] > >  > Steve Lord > slord@mathworks.com > To contact Technical Support use the Contact Us link on > http://www.mathworks.com
Steve,
Thanks for the response about the superclasses function. I guess I wasn't the only one who thought there was a need for something like this ;). However, I am disappointed that this wasn't easy to find in the documenation (at least for me). I think that starting here:
http://www.mathworks.com/help/matlab/getinformationaboutclassesandobjects.html
should have made this easy to find, i.e., the class of an object and its superclasses seem like pretty fundamental information about a class. Furthermore , the doc page for class
http://www.mathworks.com/help/matlab/ref/class.html
should include superclasses in the See Also list (especially since it points to metaclass) and the doc page for superclasses should include class in its See Also list, and the doc page for meta.class should include a link to superclasses in the defintion of the SuperClassList.
Incidentally, it seems to me that superclasses is in the wrong part of the doc anyway. It's over in "Defining Matlab Classes" when in fact it's not a function that's used to actually define a matlab class.
Thanks for all of the help, it is much appreciated.
Paul



