【问题标题】:Simulink: Use Enumeration As IndexSimulink:使用枚举作为索引
【发布时间】:2022-08-15 23:10:32
【问题描述】:

我觉得这在 C# 中非常容易,但在 Simulink 中是不可能的。我正在尝试使用枚举值作为数组索引。诀窍是:我有一个根据枚举中元素数量调整大小的数组,但它们的值是不连续的。所以我希望定义的枚举和 Simulink 代码读取 A(4) 处的值。显然,它将改为读取 A(999)。有什么方法可以得到我正在寻找的行为?

classdef Example < Simulink.IntEnumType
    enumeration
        value1 (1)
        value2 (2)
        value13 (13)
        value999 (999)
    end
end

// Below in Simulink; reputation is not good enough to post images.
A = Data Store Memory
A.InitialValue = uint16(zeros(1, length(enumeration(\'Example\'))))

// Do a Data Store Read with Indexing enabled; Index Option = Index vector (dialog)
A(Example.value999)
  • 将枚举转换为整数的数据类型转换怎么样?
  • 使用 \"Data Type Conversion\" 或 \"Cast\" 块将枚举转换为整数将为您提供整数的数值。所以: const(Example.value999) --> cast(uint16) --> display(999),而我想要的是 display(4)。

标签: matlab vector indexing simulink enumeration


【解决方案1】:

经过一个周末的实验,我想出了一个可行的解决方案:使用 Simulink 函数调用 MATLAB 函数,该函数使用“find”命令搜索正确的索引。在我的特定实例中,我将数据分配给 Data Store Memory,因此我能够将枚举索引和一个新值传递给这些块,但您也可以轻松地拥有一个输出请求索引的输入块. (我的声誉仍然太低,无法发布图片,所以希望我的文字描述就足够了。)

Data Store Memory 'A': Data type = uint16, Dimensions = length(enumeration('RegisterList'))

Simulink Function: SetValueA(ExampleEnum, NewValue)
--> MATLAB Function: SetA_Val(ExampleEnum, NewValue)
    --> function SetModbusRegister(RegisterListEnum, NewValue)

        global A;

        if(isa(ExampleEnum, 'Example'))
            A(find(enumeration('Example') == ExampleEnum, 1)) = NewValue;
        end

从那里,您可以使用 Simulink 中的 Function Caller 模块,并在您希望设置此数据的任何位置使用“函数原型”填充“SetValueA(ExampleEnum,NewValue)”。如果您希望使用向量并一次写入多个值,逻辑会变得更加复杂,但这至少是一个起点。只需修改 Simulink 和 MATLAB 函数以允许向量输入并在 MATLAB 函数中循环这些输入。

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 2016-06-26
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多