fieldnames(class_obj) vs properties(class_obj) ?

40 views (last 30 days)
Ian
Ian on 30 Jan 2026 at 20:09
Commented: Ian ungefär 24 timmar ago
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)

Answers (1)

Rik
Rik on 31 Jan 2026 at 13:29
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);
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
You should remember to use properties for objects, as a class may have a custom implementation to return a sanitized list.
  3 Comments
Rik
Rik ungefär 2 timmar ago
I presume fieldnames(struct(myclass)) does show the hidden properties? I didn't mean to suggest that fieldnames would show all properties. Just that it makes sense to me that if it doesn't throw an error, it would be equivalent to properties.
I think this would all be clearer if fieldnames would just throw an error for non-struct inputs.
Ian
Ian ungefär 15 timmar ago
What is curious to me is that in previous matlab implementations, fieldnames(some_class_object) threw an error, while it no longer does. So clearly there was a (presumably intentional) change in Matlab's implementation of the fieldnames(...) function. Yet that change is not reflected in the documentation.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!