isobject returns false for a particular class instance?
Show older comments
Hi All,
I'm confused a bit about behavior of the isobject function in MATLAB.
Are there any alternatives to check that an argument is instance of an arbitrary class? I'm using isfield to check an attribute of the instance, when it is a struct, and use isprop for the objects. But in my example below it's neither nor...
I feel like I miss something principle :-(
Let's take an example. I have an instance of Simulink.Annotation:
>> a
a =
Annotation with properties:
Name: 'Simulating Automatic Climate Control Systems'
>> class(a)
ans =
'Simulink.Annotation'
>> superclasses(a)
Superclasses for class Simulink.Annotation:
matlab.mixin.SetGet
handle
Simulink.ObjectWithFont
Simulink.Object
Simulink.DABaseObject
matlab.mixin.Heterogeneous
dynamicprops
matlab.mixin.internal.JavaVisible
da.internal.SupportsApplicationData
matlab.mixin.CustomDisplay
>> isobject(a)
ans =
logical
0
So, how could that be?
Is this because [some] Simulink objects are not MATLAB objects, as stated in the documentation?
tf = isobject(A) returns true if A is an object of a MATLAB® class. Otherwise, it returns false.
Instances of MATLAB numeric, logical, char, cell, struct, and function handle classes return false. Use isa to test for any of these types.
BTW, of course it does work for isa:
>> isa(a, 'Simulink.Object')
ans =
logical
1
Any tip?
I don't want to use explicit check for base Simulink.Object class, as in my model I also work with DataDictionaries, Stateflow, System Composer, Tests, etc.
I can probably use 'ishandle' instead of 'isobject', but not all class instances are references, some might be value instances, right?
Thanks in advance!
Nick
4 Comments
Why are you asking if the Simulink.Annotation variable is an object? What action do you want to perform differently based on the answer to that question?
I see that the Simulink.Annotation class specifically overloads the isobject function, though I'm not certain why and why it is overloaded to return false.
S = new_system;
A = Simulink.Annotation(S, "Annotation object")
which isobject(A)
isobject(A)
Compare this with:
M = magic(4);
which isobject(M)
isobject(M)
obj = groot;
which isobject(obj)
isobject(obj)
Nick
on 7 Oct 2025
Using fieldnames on an object seems to work anyway. If it doesn't you could also use a try,catch to deal with the difference automatically. Wouldn't that be an easier solution?
S = new_system;
A = Simulink.Annotation(S, "Annotation object");
fieldnames(A)
Nick
on 8 Oct 2025
Accepted Answer
More Answers (0)
Categories
Find more on Structures 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!