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
currentCommand = PositionMode
the call
Commands.(currentCommand)
evaluates to
Commands.PositionMode
and thus solves your problem in an elegant and convenient way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With