Simulink: Use Enumeration As Index

12 ビュー (過去 30 日間)
Dana Schwanke
Dana Schwanke 2022 年 8 月 14 日
コメント済み: Dana Schwanke 2022 年 8 月 16 日
I am trying to use an enumerated value as an array index. The trick is: I have an array that is sized for the number of elements in the enumeration, but their values are non-contiguous, like this:
classdef Example < uint32
enumeration
value1 (1)
value2 (2)
value13 (13)
value999 (999)
end
methods
function ind = subsindex(e)
if(isa(e, 'Example'))
ind = find(enumeration('Example') == e, 1) - 1;
else
ind = 0;
end
end
end
end
If I have a Data Store Block 'A' that is sized to length(enumeration('Example')) and I want to access A(4), I want to be able to do this by indexing A(Example.value999). With the enumeration defined above, this works perfectly in Matlab. But when I try to do the same thing in Simulink, I get an error message:
Is there a simple way to get the same behavior in Simulink?
Extra note: I thought maybe it was because I had the enumeration defined as a uint32 instead of a Simulink.IntEnumType, but changing the enumeration type actually generates an error, too:
>> F = enumeration('Example')
Error using enumeration
Method 'subsindex' in class 'Example' conflicts with the Sealed method from the superclass
definition in 'Simulink.IntEnumType'.

採用された回答

Dana Schwanke
Dana Schwanke 2022 年 8 月 15 日
I asked the same question over on StackOverflow, and after a lot of trial and error, managed to answer my own question, although maybe one of you have a better solution than I. I will reiterate the solution with images here, since I can't post them over there:
Firstly, my example above produced errors because my Example enum was based on a uint32. I was unaware that uint32 was not a valid enumeration type. Changing it to a uint16 eradicated the Simulink error message but did not produce the desired or expected behavior.
classdef Example < uint16
enumeration
value1 (1)
value2 (2)
value13 (13)
value999 (999)
end
methods
function ind = subsindex(e)
if(isa(e, 'Example'))
ind = find(enumeration('Example') == e, 1) - 1;
else
ind = 0;
end
end
end
end
With my class defined in Example.m, this is the behavior I get in MATLAB:
>> A = uint16(zeros(1, length(enumeration('Example'))))
A =
1×4 uint16 row vector
0 0 0 0
>> A(Example.value13) = 1
A =
1×4 uint16 row vector
0 0 1 0
Perfect. Beautiful. Precisely what I want. Now let's try it in Simulink.
So despite using the same enumeration defined externally in Example.m with the same subsindex, what I would define as the same coding setup (using the enumeraton as an index) produces different results. Instead of the custom subsindex being invoked, Simulink automatically casts the enumeration to its integer equivalent, generating the Simulink equivalent of an "Index out of bounds" exception.
The solution I settled upon involved using a Simulink Function to call a MATLAB function that searches for the correct index using the "find" command. In my particular instance, I was assigning the data to Data Store Memory, so I was able to just pass the enumeration index and a new value to these blocks, but you could just as easily have a single enumeration input block that spits out the requested index as a uint16.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
function SetAVal(ExampleEnum, NewValue)
global A;
if(isa(ExampleEnum, 'Example'))
A(find(enumeration('Example') == ExampleEnum, 1)) = NewValue;
end
The logic would get more complicated if you wished to use vectors and write multiple values at once, or, as previously stated, wished to assign to any generalized vector, but this is at least a starting point. It should just be a matter of modifying the Simulink and MATLAB functions to allow vector inputs and looping through those inputs in the MATLAB function.
  1 件のコメント
Dana Schwanke
Dana Schwanke 2022 年 8 月 16 日
Slight update: If your MATLAB function is set up such that you cannot use variable-length vectors in it, just replace the "find" function with the "ismember" function. Using a scalar in ismember always returns a scalar, and the MATLAB compiler won't complain about it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaming Conventions についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by