필터 지우기
필터 지우기

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