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



Re: Determining 'classname' such that isa(v,'classname') will be true
Posted:
Apr 18, 2013 9:48 AM


"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



