I have an enumerator:
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 string:
currentCommand = 'PositionMode';
I want to be able to return:
Commands.PositionMode
Is there any better solution than
methods(Static)
function obj = str2Command(string)
obj = eval(['Commands.' string]);
end
end
As with structures, you can use dynamic field names with objects.
With
the call
evaluates to
and thus solves your problem in an elegant and convenient way.