Globalize a Dictionary or containers.Maps and use it in MATLAB function block in Simulink?

4 次查看(过去 30 天)
Hello MATLAB community,
I was attempting to use a hashtable as global variable in MATLAB function block in Simulink for checking whether an ID has appeared before. However, it seems like Simulink currently doesn't support Dictionary. So, I was wondering if anyone tried this before and can suggest other approaches.
Thank you.

采纳的回答

Pratyush Swain
Pratyush Swain 2023-12-14
Hi Chang-Mu,
To achieve your goal of checking for new and redundant IDs in a MATLAB function block within Simulink, we can use "persistent" variables as a workaround.
Here's an example Simulink model. I've used "persistent" variables in a MATLAB function block to check for ID redundancy. The input "u" is a binary input (0/1) generated by a combination of "Sine Wave" and "Sign" blocks. The function block returns true for a new ID and false for a redundant ID.
Next we can create the function block in the following manner:
function y = processID(id)
% Declare the persistent variable to store the list of IDs
persistent idList;
persistent index;
% Initialize the list if it's empty %
if isempty(idList)
% Preallocate size based on time duration of simulation %
idList = zeros(1,20);
index=1;
end
% Check if the ID exists in the list %
if ~any(idList == id)
% If the ID doesn't exist, add it to the list and return 1 %
idList(index) = id;
y = 1;
% Update index %
index=index+1;
else
% ID already exists , return 0 %
y = 0;
end
end
Finally the output of the simulation can be realized as follows:
This implementation is based on the example at https://www.mathworks.com/help/simulink/ug/initialize-persistent-variables.html. Please ensure you follow the guidelines in the link before running the model, especially the "Allow direct feedthrough" property of the MATLAB function block. You can find instructions for setting block properties at https://www.mathworks.com/help/simulink/ug/matlab-function-block-properties.html.
For more information on persistent variables and other Simulink data store alternatives, please refer to:
I've also attached the Simulink model for your reference.
Hope this helps.
  2 个评论
Chang-Mu
Chang-Mu 2023-12-14
HI Pratyush,
Thank you for your reply. My current solution aligns with your answer. I actually figured out how to use Dictionary in MATLAB function block. The key is to use coder.extrinsic and include all Dictionary functions you need. For example,
coder.extrinsic("configureDictionary", "insert", "lookup", "remove");
Hope this answer helps those who have the same need as mine.
Thanks again.
Chang-Mu
Chang-Mu 2023-12-14
To note, not every keytype of Dictionary is supported in code generation even using coder.extrinsic. For example, if you declare d = configureDictionary("string", "double), you will encounter an error. so far, I only tried "double" to "double". Hope this function can be extended in the future.
Best
C-M

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by