给定以下两个类
classdef EnumClass
enumeration
enumVal1
enumVal2
end
end
classdef EnumDisplay
properties
enumValue = EnumClass.enumVal1
numberValue = 1
end
end
显示EnumClass
时,将显示值:
>> E = EnumClass.enumVal1
E =
enumVal1
但在命令窗口中显示EnumDisplay
时,枚举值将被抑制,仅显示数组大小和类。
>> C = EnumDisplay()
C =
EnumDisplay with properties:
enumValue: [1x1 EnumClass]
numberValue: 1
使枚举值显示在类特性列表中的最简单方法是什么。也就是说,有没有一种简单而通用的方法来显示类,如下所示:
>> C = EnumDisplay()
C =
EnumDisplay with properties:
enumValue: enumVal1
numberValue: 1
我怀疑这与从某个地方的matlab.mixin.CustomDisplay
类继承有关,但我希望它尽可能通用,以限制我需要为每个枚举类和/或在属性中具有枚举值的每个类执行的代码量。
部分解
我能够找出这个问题的部分解决方案,但它不是很令人满意。
classdef EnumDisplay < matlab.mixin.CustomDisplay
properties
enumValue = EnumClass.enumVal1
numberValue = 1
end
methods (Access = protected)
function groups = getPropertyGroups(This)
groups = getPropertyGroups@matlab.mixin.CustomDisplay(This);
groups.PropertyList.enumValue = char(This.enumValue);
end
end
end
现在显示如下:
>> C = EnumDisplay()
C =
EnumDisplay with properties:
enumValue: 'enumVal1'
numberValue: 1
这就差不多了,但还不完全是。我不希望枚举值包含在引号中。
发布于 2016-01-31 09:02:12
好吧,那么..。这不是最优雅的方法--当然不像使用matlab.mixin.CustomDisplay
那么优雅--但一种可能的方法是尝试自己复制该功能,以提供更多的控制。这是我在渡轮上一起黑出来的。
classdef EnumDisplay
properties
enumValue = EnumClass.enumVal1
numberValue = 1
end
methods
function disp(This)
cl = class(This) ;
fprintf(' <a href="matlab:helpPopup %s">%s</a> with properties: \n\n',cl,cl) ;
prop = properties(This) ;
len = max(cellfun(@length,prop)) ;
for ii = 1:numel(prop)
if isnumeric(This.(prop{ii}))
fmt = '%g' ;
else
fmt = '%s' ;
end
filler = char(repmat(32,1,4+len-length(prop{ii}))) ;
fprintf('%s%s: ',filler,prop{ii}) ;
fprintf(sprintf('%s \n',fmt),char(This.(prop{ii}))) ;
end
end
end
end
结果:
>> C = EnumDisplay()
C =
EnumDisplay with properties:
enumValue: enumVal1
numberValue: 1
唯一需要注意的是,这可能不是完全通用的,因为我可能没有适当地涵盖所有可能的格式fmt
。但如果你真的绝望了,也许这样的方法可以奏效。
https://stackoverflow.com/questions/22152029
复制相似问题