String to Enumeration

I have a counter:

classdef Commands

    properties
        commandString;
        readonly;
    end
    methods
        function obj = Commands(commandString, readonly)
            obj.commandString = commandString;
            obj.readonly= readonly;
        end
    end
    enumeration
        PositionMode('p', false)
        TravelDistance('s', false)
    end
end

and I have a line:

currentCommand = 'PositionMode';

I want to be able to return:

Commands.PositionMode

Is there a better solution than

methods(Static)
    function obj = str2Command(string)
        obj = eval(['Commands.' string]);
    end
end
+5
source share
1 answer

As with structures, you can use dynamic field names with objects.

WITH

currentCommand = PositionMode

call

Commands.(currentCommand)

estimated as

Commands.PositionMode

and thus solves your problem in an elegant and convenient way.

+5
source

All Articles