isobject returns false for a particular class instance?

32 views (last 30 days)
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
Rik
Rik 2 minutes ago
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)
ans = 36×1 cell array
{'Position' } {'InternalMargins' } {'IsImage' } {'FixedHeight' } {'FixedWidth' } {'HorizontalAlignment' } {'VerticalAlignment' } {'ForegroundColor' } {'BackgroundColor' } {'Text' } {'PlainText' } {'DropShadow' } {'AnnotationType' } {'Interpreter' } {'TeXMode' } {'ShowInLibBrowser' } {'MarkupType' } {'ClickFcn' } {'LoadFcn' } {'DeleteFcn' } {'UseDisplayTextAsClickCallback'} {'UserData' } {'Path' } {'FontName' } {'FontSize' } {'FontWeight' } {'FontAngle' } {'Selected' } {'Name' } {'Tag' }
Nick
Nick 22 minutes ago
Thanks, @Rik, I also considered this approach! You're right, at the end, it could be an easier one, as I also can use 'methods' or 'ismethod' on anything, including structs or primitive instances, and this way I can simplify all the processing without differentiating structs from class instances.

Sign in to comment.

Accepted Answer

Rik
Rik on 7 Oct 2025 at 13:33
Edited: Rik about 18 hours ago
You can go the other way around. Adapt the list as needed. The last time a new basic type was added was the string class. I don't expect a new one any time soon.
obj=@sin;
isSortOfObject(obj)
ans = logical
0
obj=1;
tf = logical
1
1
isSortOfObject(obj)
ans = logical
0
obj=figure;
isSortOfObject(obj),close(obj)
ans = logical
0
function tf=isSortOfObject(obj)
% Use this as an alternative to isobject
tf = ~isBasicType(obj);
end
function tf=isBasicType(obj)
% See this page for the fundamental classes:
% https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html
BasicTypes={...
'double','single','half',...
'logical',...
'function_handle','handle',...
'struct','cell','table',...% what about timetables?
'char','string',...
'uint8','uint16','uint32','uint64',...
'int8' , 'int16', 'int32', 'int64'};
tf = false;
for n=1:numel(BasicTypes)
if isa(obj,BasicTypes{n})
tf = true;
return
end
end
return
%alternative function:
tf = isa(obj,'numeric') || ...
ismember(class(obj),{'logical','string','char','table','cell','struct','function_handle'})
end
  2 Comments
Nick
Nick on 7 Oct 2025 at 14:12
Hi @Rik, thank you for your support!
I think I can work with that!
I'll try it out and confirm your answer.
May I ask you to clarify:
  • in your example figure is not isSortOfObject, why so? It reports true on isobject, it also has bunch of properties and methods
BTW, isobject("hello") returns true, but I'd rather agree with you and leave string as a basic/primitive type :-)
Would you recommend me to create a ticket on documentation or API improvement, or I'm wrong in assumption, that 'isobject' should work closer to what your 'isSortOfObject' is doing?
Thank you!
Nick
Rik
Rik on 7 Oct 2025 at 14:28

I don't think there is a single correct answer to your last question. That is also mostly the answer your first question: I included handle in the list of types that should not be considered an object. Now you mention it, the reverse makes more sense. Feel free to edit the list of types when using this implementation.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!