isobject returns false for a particular class instance?
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nick
el 7 de Oct. de 2025 a las 7:12
Comentada: Nick
el 8 de Oct. de 2025 a las 7:57
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
Rik
hace alrededor de 5 horas
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)
Respuesta aceptada
Rik
el 7 de Oct. de 2025 a las 13:33
Editada: Rik
hace alrededor de 23 horas
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)
obj=1;
isSortOfObject(obj)
obj=figure;
isSortOfObject(obj),close(obj)
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
Rik
el 7 de Oct. de 2025 a las 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!