如何在MATLAB对象中显示枚举值 [英] How can I display enumeration value in MATLAB object

查看:386
本文介绍了如何在MATLAB对象中显示枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下两个类

classdef EnumClass

    enumeration
        enumVal1
        enumVal2
    end
end


classdef EnumDisplay

    properties
        enumValue = EnumClass.enumVal1
        numberValue = 1
    end
end

显示EnumClass时,显示值:

>> E = EnumClass.enumVal1

E = 

    enumVal1

但是在命令窗口中显示EnumDisplay时,枚举值被抑制,并且仅显示数组大小和类.

but when displaying EnumDisplay in the command window, the enumeration value is suppressed, and only the array size and class are displayed.

>> C = EnumDisplay()

C =

  EnumDisplay with properties:

      enumValue: [1x1 EnumClass]
    numberValue: 1

在类属性列表中显示枚举值的最简单方法是什么. IE.有没有一种简单而通用的方法来显示该类,如下所示:

What is the easiest way to have the enumeration value displayed in the class property list. I.e. is there an easy and general way to have the class displayed as follows:

>> C = EnumDisplay()

C =

  EnumDisplay with properties:

      enumValue: enumVal1
    numberValue: 1

我怀疑这与从某个地方的matlab.mixin.CustomDisplay类继承有关,但是我希望这尽可能通用,以限制我需要对每个枚举类进行的编码量,和/或每个在属性中具有枚举值的类.

I suspect that this has something to do with inheriting from the matlab.mixin.CustomDisplay class somewhere, but I want this to be as general as possible, to limit the amount of coding i need to do for each enumeration class, and/or each class that has an enumeration value in a property.

我能够找到部分解决此问题的方法,但并不十分令人满意.

I was able to figure out a partial solution to this problem, but it is not quite satisfactory.

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

这几乎存在,但不完全是.我不希望枚举值用引号引起来.

This is almost there, but not quite. I don't want the enumerated value to be in quotations.

推荐答案

好吧...这不是最优雅的方法-肯定不如使用matlab.mixin.CustomDisplay优雅-但一种可能性是尝试以一种可以给您更多控制的方式自己复制该功能.这是我一起在渡轮上砍下的东西...

Okay, well... this isn't the most elegant approach -- certainly not as elegant as using matlab.mixin.CustomDisplay -- but one possibility is to try to replicate that functionality yourself, in a way that gives you more control. Here is what I hacked together on the ferry...

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.但是,如果您真的很绝望,也许这样的事情会起作用.

Only catch is that this may not be fully generic because I may not have appropriately covered all of the possible formats fmt. But if you are really desperate, maybe something like this will work.

这篇关于如何在MATLAB对象中显示枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆