che*_*Out 5 matlab enumeration
我有一个普查员:
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
Run Code Online (Sandbox Code Playgroud)
我有一个字符串:
currentCommand = 'PositionMode';
Run Code Online (Sandbox Code Playgroud)
我希望能够回归:
Commands.PositionMode
Run Code Online (Sandbox Code Playgroud)
有没有更好的解决方案呢?
methods(Static)
function obj = str2Command(string)
obj = eval(['Commands.' string]);
end
end
Run Code Online (Sandbox Code Playgroud)
与结构一样,您可以将动态字段名称与对象一起使用.
同
currentCommand = PositionMode
Run Code Online (Sandbox Code Playgroud)
电话
Commands.(currentCommand)
Run Code Online (Sandbox Code Playgroud)
评估为
Commands.PositionMode
Run Code Online (Sandbox Code Playgroud)
从而以优雅便捷的方式解决您的问题.