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)
Mostrar comentarios más antiguos
Dion Timmermann
el 5 de Dic. de 2022
Comentada: Dion Timmermann
el 13 de Dic. de 2022
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?
0 comentarios
Respuesta aceptada
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
Más respuestas (0)
Ver también
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!