自定义枚举类的显示
我想使用 matlab.mixin.CustomDisplay
自定义枚举类的显示。
I'd like to customize the display of an enumeration class using matlab.mixin.CustomDisplay
.
如果我有一个常规(非枚举)类,如下所示:
If I have a regular (non-enumeration) class such as the following:
classdef test < handle & matlab.mixin.CustomDisplay
properties
value
end
methods
function obj = test(value)
obj.value = value;
end
end
methods (Access = protected)
function displayScalarObject(obj)
disp(['hello ', num2str(obj.value)])
end
end
end
然后一切正常 - 例如,
then everything works fine - for example,
>> a = test(1)
a =
hello 1
但是如果我有枚举类如下(注意添加枚举
块):
But if I have an enumeration class such as the following (note the addition of the enumeration
block):
classdef test < handle & matlab.mixin.CustomDisplay
properties
value
end
methods
function obj = test(value)
obj.value = value;
end
end
methods (Access = protected)
function displayScalarObject(obj)
disp(['hello ', num2str(obj.value)])
end
end
enumeration
enum1(1)
end
end
然后显示不是自定义的 - 例如,
then the display is not customized - for example,
>> a = test.enum1
a =
enum1
使用调试器,我可以看到我的 displayScalarObject
方法从未被调用。实现 matlab.mixin.CustomDisplay
的其他方法,如 displayNonScalarObject
等似乎没有帮助 - 这些从来没有得到调用。
Using the debugger, I can see that my displayScalarObject
method is never called. Implementing other methods of matlab.mixin.CustomDisplay
such as displayNonScalarObject
and so on doesn't seem to help - these never get called either.
发生了什么事?对于枚举类,do disp
和显示
的工作方式不同,这样一来,任何被 matlab.mixin.CustomDisplay
只是被忽略?
What's going on? Do disp
and display
work differently for enumeration classes, in such a way that anything that's overridden by matlab.mixin.CustomDisplay
just gets ignored?
有没有办法获得定制的显示与 matlab。 mixin.CustomDisplay
,但使用枚举类?
Is there a way to get a customized display with matlab.mixin.CustomDisplay
, but using an enumeration class?
PS我可以直接超载 disp
和/或在不继承
,而这个工作正常。但是,如果可能,我希望使用 matlab.mixin.CustomDisplay
的枚举类中显示 matlab.mixin.CustomDisplay
。
PS I am able to directly overload disp
and/or display
on an enumeration class that does not inherit from matlab.mixin.CustomDisplay
, and this works fine. But I'm looking to use matlab.mixin.CustomDisplay
if possible.
我会回答我自己的问题,以防其他人感兴趣。
I'll answer my own question in case anyone else is interested.
该问题是参考MATLAB R2014a提出的。在R2014b中,在 matlab.mixin.CustomDisplay
的文档中添加了一个附加注释,使其明确表示不可能使用 matlab。 mixin.CustomDisplay
为枚举类派生自定义显示,并表示最好的方法是超载 disp
。
The question was asked with reference to MATLAB R2014a. In R2014b, an additional note has been added to the documentation for matlab.mixin.CustomDisplay
, making it explicit that it is not possible to use matlab.mixin.CustomDisplay
to derive a custom display for enumeration classes, and suggesting that the best approach is to overload disp
.