Simulink: Use Enumeration As Index

10 Ansichten (letzte 30 Tage)
Iam Hamidd
Iam Hamidd am 13 Aug. 2022
Bearbeitet: Walter Roberson am 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)

Antworten (1)

dpb
dpb am 13 Aug. 2022
Bearbeitet: dpb am 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.

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by