matlab - 枚举的字符串

标签 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

我有一个字符串:

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/

相关文章:

Matlab:基于子矩阵 reshape 大矩阵的最佳方法是什么

java - 返回与字符串参数同名的枚举是什么意思?

Delphi for..in循环设置枚举顺序

matlab - 将类别值快速扩展到矩阵中的归一化特征向量行

performance - 使用 MATLAB 高效创建特定矩阵

python - 如何将整数日期格式转换为 YYYYMMDD?

string - 如何从matlab中的字符串中获取数字

java - 在java中,这样的枚举类型编译成什么?

c# - 集合被修改;枚举操作可能无法执行 - 为什么?

algorithm - 什么是自然数和有效的简单类型 lambda 演算项之间的映射?