Azzera filtri
Azzera filtri

Simulink: Use Enumeration As Index

8 visualizzazioni (ultimi 30 giorni)
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'.

Risposta accettata

Dana Schwanke
Dana Schwanke il 15 Ago 2022
I asked the same question over on StackOverflow, and after a lot of trial and error, managed to answer my own question, although maybe one of you have a better solution than I. I will reiterate the solution with images here, since I can't post them over there:
Firstly, my example above produced errors because my Example enum was based on a uint32. I was unaware that uint32 was not a valid enumeration type. Changing it to a uint16 eradicated the Simulink error message but did not produce the desired or expected behavior.
classdef Example < uint16
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
With my class defined in Example.m, this is the behavior I get in MATLAB:
>> A = uint16(zeros(1, length(enumeration('Example'))))
A =
1×4 uint16 row vector
0 0 0 0
>> A(Example.value13) = 1
A =
1×4 uint16 row vector
0 0 1 0
Perfect. Beautiful. Precisely what I want. Now let's try it in Simulink.
So despite using the same enumeration defined externally in Example.m with the same subsindex, what I would define as the same coding setup (using the enumeraton as an index) produces different results. Instead of the custom subsindex being invoked, Simulink automatically casts the enumeration to its integer equivalent, generating the Simulink equivalent of an "Index out of bounds" exception.
The solution I settled upon involved using a Simulink Function to call a MATLAB function that searches for the correct index using the "find" command. In my particular instance, I was assigning the data to Data Store Memory, so I was able to just pass the enumeration index and a new value to these blocks, but you could just as easily have a single enumeration input block that spits out the requested index as a uint16.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
function SetAVal(ExampleEnum, NewValue)
global A;
if(isa(ExampleEnum, 'Example'))
A(find(enumeration('Example') == ExampleEnum, 1)) = NewValue;
end
The logic would get more complicated if you wished to use vectors and write multiple values at once, or, as previously stated, wished to assign to any generalized vector, but this is at least a starting point. It should just be a matter of modifying the Simulink and MATLAB functions to allow vector inputs and looping through those inputs in the MATLAB function.
  1 Commento
Dana Schwanke
Dana Schwanke il 16 Ago 2022
Slight update: If your MATLAB function is set up such that you cannot use variable-length vectors in it, just replace the "find" function with the "ismember" function. Using a scalar in ismember always returns a scalar, and the MATLAB compiler won't complain about it.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by