Simulink: Use Enumeration As Index

10 views (last 30 days)
Iam Hamidd
Iam Hamidd on 13 Aug 2022
Edited: Walter Roberson on 13 Aug 2022
I feel like this is something that'd be absurdly easy in C# but is impossible in Simulink. 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. So I want the defined enumeration and Simulink code to read the value at A(4). Obviously, it will instead read A(999). Any way to get the behavior I'm looking for?
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)

Answers (1)

dpb
dpb on 13 Aug 2022
Edited: dpb on 13 Aug 2022
Try something like
idx=find(ismember(enumeration(Example),Example.value999));
A(idx)
SIDEBAR:
MATLAB enumeration classes are weird things -- they are nothing at all like a classical C-like enumeration which is, essentially nothing but a macro #define with scope. In C you get just the constant; in MATLAB you get the whole class thingie as an object that just displays its value and MATLAB functions can get its value, but it's not just the constant. I tried it with constants for an ActiveX class to interact w/ Excel to be able to use named constants in the MATLAB code a la the VBA constants -- crash and burn; MATLAB doesn't send the value but the object.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!