isobject returns false for a particular class instance?

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 comentarios

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")
A =
Annotation with properties: Name: 'Annotation object'
which isobject(A)
isobject is a built-in method % Simulink.Annotation method
isobject(A)
ans = logical
0
Compare this with:
M = magic(4);
which isobject(M)
built-in (/MATLAB/toolbox/matlab/datatypes/isobject)
isobject(M)
ans = logical
0
obj = groot;
which isobject(obj)
built-in (/MATLAB/toolbox/matlab/datatypes/isobject)
isobject(obj)
ans = logical
1
Nick
Nick el 7 de Oct. de 2025
I'm working on integration between MATLAB and ALM solutions, where the data from MATLAB could be dumped in a textual form into a WorkItem for further analyze, traceability, change impact, etc. on the ALM side.
For example, if I need to push information about a Simulink.Subsystem, I can do extracting of the dialog parameters, ports descriptions, etc., even recursively. And some of the properties are objects, others are structs, thirds are arrays of something, primitive types, etc.
To read the properties of some nested objects, I need to get the names of those, if that is a 'complex' thing. I need to know how to access them - as field of a struct, or as a property of a class instance.
For reading generic structs I use 'fieldnames'. If it's a class I tried to use 'isobject' and then 'properties'.
Simulink.Annotation was just an example - actually I figured out that all(?) Simulink objects return false for isobject, probably they all override the method, as you mentioned.
Side note: it's not only about Simulink - I'm processing Stateflow, System Composer, Data Dictionary, and of course some 'sattellite' data, like Stereotypes, custom attributes, slreq.Requirements, Links, etc.etc.
Thanks for asking!
Nick
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 el 8 de Oct. de 2025
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 7 de Oct. de 2025
Editada: Rik el 7 de Oct. de 2025
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 comentarios

Nick
Nick el 7 de Oct. de 2025
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 el 7 de Oct. de 2025

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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2025a

Etiquetas

Preguntada:

el 7 de Oct. de 2025

Comentada:

el 8 de Oct. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by