我有一个枚举器:
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
我有一个字符串:
currentCommand = 'PositionMode';
我希望能够返回:
Commands.PositionMode
有没有比
更好的解决办法methods(Static)
function obj = str2Command(string)
obj = eval(['Commands.' string]);
end
end
最佳答案
对于结构,您可以使用 dynamic field names与对象。
与
currentCommand = PositionMode
电话
Commands.(currentCommand)
评估为
Commands.PositionMode
从而以优雅便捷的方式解决了您的问题。
关于matlab - 枚举的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11447991/