Simulink: Use Enumeration As Index

조회 수: 10 (최근 30일)
Iam Hamidd
Iam Hamidd 2022년 8월 13일
편집: Walter Roberson 2022년 8월 13일
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