Custom class property and method attributes

5 visualizaciones (últimos 30 días)
Thomas Satterly
Thomas Satterly el 10 de Sept. de 2019
Comentada: Thomas Satterly el 4 de Nov. de 2019
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?
  2 comentarios
Adam
Adam el 4 de Nov. de 2019
I'm not entirely sure what it is you are wanting, and didn't notice the question at the time, though as you seem to have found an answer now I guess it doesn't matter. I'd have thought dependent properties could have been utilised to achieve this though.
I did implement my own class called a JustInTimeProperty to store certain data in my classes too as I have cases where I want the property to only be calculated when first asked for, but then stored thereafter, rather than a pure dependent property which would always recalculate or a regular property which would have to be calculated upfront even if it is never actually asked for.
I can't remember the exact implementation i had for these and it isn't the same scenario you want anyway, but their usage did make use of dependent properties, paired with private properties.
Thomas Satterly
Thomas Satterly el 4 de Nov. de 2019
I did a poor job wording what I wanted. Basically, I wanted a class property that, when saved, would save some key attribute of the object referenced by the property rather than the property itself. Then, upon loading, the attribute would br translated back into the originally saved object (which would have been saved and loaded separately). I have an application with highly interconnected objects that store multiple references to each other, and there were issues of duplicate object creation when saving and loading with the default methods because the save and load processes are not aware of the entire context that each object lives in.

Iniciar sesión para comentar.

Respuesta aceptada

Thomas Satterly
Thomas Satterly el 4 de Nov. de 2019
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.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by