How can I add a custom compact display in a way that does not produce errors in MATLAB versions prior to 2021b?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2022-12-5
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Class File Organization 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!