How to get name of an enumeration in MATLAB





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







7















I define an enumerated type in MATLAB



classdef(Enumeration) Color < Simulink.IntEnumType
enumeration
RED(0),
GREEN(1),
BLUE(2),
end
end


I can assign it:



>> x = Color.RED    
x =
RED


I can display it like this:



>> disp(x)
RED


or like this



>> x.display()
x =
RED


How can I get access to that name ("RED") as a string?



In other words I'm lookin for something like:



s = x.toString()


or



s = tostring(x)


both of which do not work.










share|improve this question































    7















    I define an enumerated type in MATLAB



    classdef(Enumeration) Color < Simulink.IntEnumType
    enumeration
    RED(0),
    GREEN(1),
    BLUE(2),
    end
    end


    I can assign it:



    >> x = Color.RED    
    x =
    RED


    I can display it like this:



    >> disp(x)
    RED


    or like this



    >> x.display()
    x =
    RED


    How can I get access to that name ("RED") as a string?



    In other words I'm lookin for something like:



    s = x.toString()


    or



    s = tostring(x)


    both of which do not work.










    share|improve this question



























      7












      7








      7


      1






      I define an enumerated type in MATLAB



      classdef(Enumeration) Color < Simulink.IntEnumType
      enumeration
      RED(0),
      GREEN(1),
      BLUE(2),
      end
      end


      I can assign it:



      >> x = Color.RED    
      x =
      RED


      I can display it like this:



      >> disp(x)
      RED


      or like this



      >> x.display()
      x =
      RED


      How can I get access to that name ("RED") as a string?



      In other words I'm lookin for something like:



      s = x.toString()


      or



      s = tostring(x)


      both of which do not work.










      share|improve this question
















      I define an enumerated type in MATLAB



      classdef(Enumeration) Color < Simulink.IntEnumType
      enumeration
      RED(0),
      GREEN(1),
      BLUE(2),
      end
      end


      I can assign it:



      >> x = Color.RED    
      x =
      RED


      I can display it like this:



      >> disp(x)
      RED


      or like this



      >> x.display()
      x =
      RED


      How can I get access to that name ("RED") as a string?



      In other words I'm lookin for something like:



      s = x.toString()


      or



      s = tostring(x)


      both of which do not work.







      matlab oop enums






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 27 '13 at 20:37









      Amro

      116k19210393




      116k19210393










      asked Feb 22 '10 at 17:33









      PhilippPhilipp

      2,10972853




      2,10972853
























          2 Answers
          2






          active

          oldest

          votes


















          8














          You can use:



          » str = char(Color.RED)
          str =
          RED
          » class(str)
          ans =
          char


          You can even override the default behaviour:



          classdef(Enumeration) Color < int32
          enumeration
          RED(0)
          GREEN(1)
          BLUE(2)
          end

          methods
          function s = char(obj)
          s = ['Color ' num2str(obj)];
          %# or use a switch statement..
          end

          function disp(obj)
          disp( char(obj) )
          end
          end
          end


          and now:



          » char(Color.BLUE)
          ans =
          Color 2





          share|improve this answer


























          • note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

            – Amro
            Feb 22 '10 at 18:46













          • Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

            – gnovice
            Feb 22 '10 at 19:41






          • 1





            @gnovice: it is returning "RED" as expected

            – Amro
            Feb 22 '10 at 19:59











          • Ah, good. Just checking. ;)

            – gnovice
            Feb 22 '10 at 21:38



















          0














          A different approach, generic, in your calling entitiy:



          strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))


          This way you can save the class specific implementation of disp() and/or char().






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2312774%2fhow-to-get-name-of-an-enumeration-in-matlab%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            8














            You can use:



            » str = char(Color.RED)
            str =
            RED
            » class(str)
            ans =
            char


            You can even override the default behaviour:



            classdef(Enumeration) Color < int32
            enumeration
            RED(0)
            GREEN(1)
            BLUE(2)
            end

            methods
            function s = char(obj)
            s = ['Color ' num2str(obj)];
            %# or use a switch statement..
            end

            function disp(obj)
            disp( char(obj) )
            end
            end
            end


            and now:



            » char(Color.BLUE)
            ans =
            Color 2





            share|improve this answer


























            • note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

              – Amro
              Feb 22 '10 at 18:46













            • Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

              – gnovice
              Feb 22 '10 at 19:41






            • 1





              @gnovice: it is returning "RED" as expected

              – Amro
              Feb 22 '10 at 19:59











            • Ah, good. Just checking. ;)

              – gnovice
              Feb 22 '10 at 21:38
















            8














            You can use:



            » str = char(Color.RED)
            str =
            RED
            » class(str)
            ans =
            char


            You can even override the default behaviour:



            classdef(Enumeration) Color < int32
            enumeration
            RED(0)
            GREEN(1)
            BLUE(2)
            end

            methods
            function s = char(obj)
            s = ['Color ' num2str(obj)];
            %# or use a switch statement..
            end

            function disp(obj)
            disp( char(obj) )
            end
            end
            end


            and now:



            » char(Color.BLUE)
            ans =
            Color 2





            share|improve this answer


























            • note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

              – Amro
              Feb 22 '10 at 18:46













            • Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

              – gnovice
              Feb 22 '10 at 19:41






            • 1





              @gnovice: it is returning "RED" as expected

              – Amro
              Feb 22 '10 at 19:59











            • Ah, good. Just checking. ;)

              – gnovice
              Feb 22 '10 at 21:38














            8












            8








            8







            You can use:



            » str = char(Color.RED)
            str =
            RED
            » class(str)
            ans =
            char


            You can even override the default behaviour:



            classdef(Enumeration) Color < int32
            enumeration
            RED(0)
            GREEN(1)
            BLUE(2)
            end

            methods
            function s = char(obj)
            s = ['Color ' num2str(obj)];
            %# or use a switch statement..
            end

            function disp(obj)
            disp( char(obj) )
            end
            end
            end


            and now:



            » char(Color.BLUE)
            ans =
            Color 2





            share|improve this answer















            You can use:



            » str = char(Color.RED)
            str =
            RED
            » class(str)
            ans =
            char


            You can even override the default behaviour:



            classdef(Enumeration) Color < int32
            enumeration
            RED(0)
            GREEN(1)
            BLUE(2)
            end

            methods
            function s = char(obj)
            s = ['Color ' num2str(obj)];
            %# or use a switch statement..
            end

            function disp(obj)
            disp( char(obj) )
            end
            end
            end


            and now:



            » char(Color.BLUE)
            ans =
            Color 2






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 22 '10 at 19:59

























            answered Feb 22 '10 at 18:42









            AmroAmro

            116k19210393




            116k19210393













            • note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

              – Amro
              Feb 22 '10 at 18:46













            • Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

              – gnovice
              Feb 22 '10 at 19:41






            • 1





              @gnovice: it is returning "RED" as expected

              – Amro
              Feb 22 '10 at 19:59











            • Ah, good. Just checking. ;)

              – gnovice
              Feb 22 '10 at 21:38



















            • note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

              – Amro
              Feb 22 '10 at 18:46













            • Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

              – gnovice
              Feb 22 '10 at 19:41






            • 1





              @gnovice: it is returning "RED" as expected

              – Amro
              Feb 22 '10 at 19:59











            • Ah, good. Just checking. ;)

              – gnovice
              Feb 22 '10 at 21:38

















            note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

            – Amro
            Feb 22 '10 at 18:46







            note: since I dont have simulink, I tested the above using the definition: classdef(Enumeration) Color < int32

            – Amro
            Feb 22 '10 at 18:46















            Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

            – gnovice
            Feb 22 '10 at 19:41





            Did you test what str actually was? I can't test it right now, but I think this might just convert the integer representation of the enumerated type to a char (i.e. char(0)).

            – gnovice
            Feb 22 '10 at 19:41




            1




            1





            @gnovice: it is returning "RED" as expected

            – Amro
            Feb 22 '10 at 19:59





            @gnovice: it is returning "RED" as expected

            – Amro
            Feb 22 '10 at 19:59













            Ah, good. Just checking. ;)

            – gnovice
            Feb 22 '10 at 21:38





            Ah, good. Just checking. ;)

            – gnovice
            Feb 22 '10 at 21:38













            0














            A different approach, generic, in your calling entitiy:



            strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))


            This way you can save the class specific implementation of disp() and/or char().






            share|improve this answer






























              0














              A different approach, generic, in your calling entitiy:



              strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))


              This way you can save the class specific implementation of disp() and/or char().






              share|improve this answer




























                0












                0








                0







                A different approach, generic, in your calling entitiy:



                strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))


                This way you can save the class specific implementation of disp() and/or char().






                share|improve this answer















                A different approach, generic, in your calling entitiy:



                strtrim(matlab.unittest.diagnostics.ConstraintDiagnostic.getDisplayableString(Color.BLUE))


                This way you can save the class specific implementation of disp() and/or char().







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 3 at 9:28

























                answered Jan 3 at 9:07









                braggPeaksbraggPeaks

                591516




                591516






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2312774%2fhow-to-get-name-of-an-enumeration-in-matlab%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules