How can I add a custom compact display in a way that does not produce errors in MATLAB versions prior to 2021b?

1 visualización (últimos 30 días)
MATLAB 2021b added matlab.mixin.CustomCompactDisplayProvider that alows to define a custom compat display for a class. I would like to add support for a compat custom display to several classes in a Toolbox I am working on. However, that Toolbox needs to also work with older MATLAB version (ideally from 2018b until today). I am fine if the custom compact display only works in the newer version.
To implement a custom compact display, I need to derive my class from matlab.mixin.CustomCompactDisplayProvider. However, in MATLAB versions prior to 2021b, that class does not exist. Therefore, loading class that is derived from matlab.mixin.CustomCompactDisplayProvider in older matlab versions causes an error.
Is there a way to add a custom compact display that does not result in an error in older matlab versions?

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Dic. de 2022
Have a class that has a property.
In the code that initializes the property, use verlessthan to test whether you are running before the relevant version. If you are are, then initialize the property to a class object belonging to a class that does not derive from the custom display; else initialize to an object that does inherit from it.
You cannot use verlessthan to control inheritance, and as best I recall you cannot do dynamic inheritance... but you can control which function you execute.
If a class is never loaded because no line loading an object of the class is ever executed, then it does not matter that the class derives from a class that does not exist in the version. As long as both branches of the verlessthan are valid static syntax. For example I am not sure it would work to verlessthan to prevent execution of code which uses "" syntax in versions too old for it, as "" required changes to the parser. But
if verlessthan(etc)
obj.prop1 = Class1Constructor(stuff);
else
obj.prop1 = Class1WithCustom(stuff) ;
end
  1 comentario
Dion Timmermann
Dion Timmermann el 13 de Dic. de 2022
Thank you Walter! This would indeed be a possible way.
The main issue I have with your solution is that I have to put some object conversion at every point where these properties are created or assigned. While I can of cause wrap the if-statement in a seprate function, it still requires me to put display-related function calls in code that does mathematical calculations. For ease of code maintainance, I would prefer to seperate the display-related code from the mathematical calculations.
I have now decided to use a different approach. I defined a custom display for the outer object so it uses my own displayInline method. This is certainly not ideal, but allows for a seperation between display methods and mathematical calculations.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Class File Organization en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by