MATLAB: Custom class property and method attributes

attributeattributesframeworkMATLAB

I'd like to be able to define my own attributes in classes, but I'm not finding any documentation as to how that might be done. My main goal is to be able to create a property attribute, "UseAsReference", that marks object properties that need to be saved and loaded via custom methods because they are references to other Matlab handle objects. I'm hoping to accomplish this with property attributes and a superclass rather than defining an interface so that I don't have to worry about maintaining a constant list of reference properties and custom save/load functions for each implementing member. The pages for property and method attributes note that inheriting from certian frameworks (e.g., the UnitTest framework) will open up more attributes that a class can use. Is it possible to replicate this functionality?

Best Answer

So Matlab doesn't support this type of thing, which can't really be too surprising. The best way I've found to get around this is to just redefine saveobj() and loadobj() methods, while having all "special" data stored in a dedicated struct that is purpose built for properties that can't be saved normally. For example, if is a property that reference another object, but you just want to save a "reference" (like through a UID), make the property transient, and saveobj() and loadobj() are then responsible for saving the UID to the dedicated save struct and attempting to find the old reference through the saved UID when loading.