Set callback Function to Multiple Blocks

Hello !
I want to set the same callback OpenFcn function to multiple Subsytem blocks, but I can figure it out how. Can you help me please ?
Thank you so much!

回答 (1 件)

Suman
Suman 2025 年 6 月 19 日

0 投票

You can programmatically assign the same "OpenFcn" callback to multiple Subsystem blocks in Simulink using a MATLAB script.
  1. Create a string containing all the code for the callback.
  2. Find all the subsytem blocks.
  3. Use set_param to set the callback function.
For example:
callbackLines = {
'disp(''You opened this subsystem'');'
'x = 42;'
'y = x * 2;'
'fprintf(''Computed value: %d\n'', y);'
};
callbackStr = strjoin(callbackLines, '\n');
subsystems = find_system(gcs, 'BlockType', 'SubSystem');
for i = 1:length(subsystems)
set_param(subsystems{i}, 'OpenFcn', callbackStr);
end
Hope this helps!

カテゴリ

ヘルプ センター および File ExchangeSubsystems についてさらに検索

質問済み:

2019 年 8 月 23 日

回答済み:

2025 年 6 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by