Simulink: Use Enumeration As Index
Show older comments
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'.
Accepted Answer
More Answers (0)
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

