Simulink: Use Enumeration As Index

10 次查看(过去 30 天)
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)

回答(1 个)

dpb
dpb 2022-8-13
编辑:dpb 2022-8-13
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.

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by