fieldnames(class_obj) vs properties(class_obj) ?
26 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Question re using fieldnames(obj) vs properties(obj) on Matlab class objects:
In earlier versions of matlab, fieldnames(obj) only worked on struct objects; one had to use properties(obj) to get the names of properties in a Matlab class object. (IIRC, as late as 2015x)
It appears that fieldnames(obj) now works for class objects as well. (this on R2024b*, tho IIRC it worked in 24a as well, possibly earlier)
However, R2025b documentation doesn't mention using fieldnames(...) for class objects, and lists properties(obj) as the function to get class object property names. Is this an undocumented feature that may go away, or is this intended behaviour going forward for fieldnames(...) , and documentation just hasn't kept up?
I note that fieldnames(...) cannot be stepped into with the debugger, so am guessing it must be implemented in underlying Java of mex code.
Also of note is that fieldnames(...) has an optional parameter of '-full' which apparently returns inheritance information on java and com objects (though apparently not on inheritance of Matlab parent/child classes).
* (Yes, I know it's 2026, not 2024...just current w/ linux version available on university's HPCC)
0 Commenti
Risposte (1)
Rik
circa 17 ore fa
Did you check the release notes? If they don't mention this as a dev direction, you should consider this undocumented.
The underlying reason is that objects in Matlab share a lot of features with structs. You can call struct on an object to reveal most (usually all) properties, even if they are hidden:
struct(figure);
You should remember to use properties for objects, as a class may have a custom implementation to return a sanitized list.
1 Commento
Matt J
circa 15 ore fa
Modificato: Matt J
circa 15 ore fa
The underlying reason is that objects in Matlab share a lot of features with structs. You can call struct on an object to reveal most (usually all) properties, even if they are hidden:
That doesn't quite explain it for me. Simple tests show that fieldnames(obj) is not equivalent to fieldnames(struct(obj)). As demonstrated below, fieldnames(obj) does not reveal hidden or protected properties.
fieldnames(myclass)
ans =
1×1 cell array
{'c'}
properties(myclass)
Properties for class myclass:
c
classdef myclass
properties (GetAccess=protected)
a,b
end
properties
c
end
end
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!