How can I display enumeration value in MATLAB object

痴心易碎 提交于 2019-12-04 04:20:28

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

Result:

>> C = EnumDisplay()

C = 

  EnumDisplay with properties: 

      enumValue: enumVal1 
    numberValue: 1 

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!