Simulink callback that is always called on CTRL+D even when nothing changed

My goal:
I want to generate *.sldd files automatically based on local variables and current configurations, which will make our development process much easier, as it allows for a more modular approach without requiring any manual selections.
The setup:
I work with many referenced models, which all point to their own data dictionary and config set (also generated in the matlab scripts) and variant models to allow for different setups.
The problem:
If I change a variable within one of the scripts, that would adjust an sldd file, I would like to it be run on project open (that's easy through the preLoadFnc) and on compile. For the latter, I have the option of using the initFcn callback for each model, but it is only triggered on the top level (the harness) and on reference models that have changes and are getting recompiled, otherwise the initFnc will not be called. But this results in the problem that a change in the matlab script will not be applied to the sldd and hence Simulink will simply use the old parameters.
Solution approaches for now (without success):
I tried using the Model dependencies (in the Model Referencing settings), and include all the scripts responsible for the SLDD. So if the script changes, the model is recompiled and we have the new settings. But there are two problems with that:
  1. The model will always be recompiled (even if I only change a Simulink Parameter that shoudn't hit recompile, or even if I change a variable that's not even used), bypassing the normal is effected chain
  2. Sometimes the script itself doesn't actually change, but it's output, as it depends on a enum based config in the workspace. And watching for changes in the config is not desired, as it would hit recompile on the whole project using this method and that would take for ever
The other obvious approach is using a top-down structure, in the top level initFcn call all scripts that also generate/update the scripts of all children, but that I need to have the awareness which children are being used and can't use the models separatly outside that harness as they do not load their own data anymore. I would like to avoid this.
Desired solution:
Have some callback that is being called right before the automatic dependency checker, this way I can adapt the sldd if necessary in the script, hence load some different data because the config now wants for example different sensor parameters and then the automatic dependency checker can run as normal and judge if this should trigger a recompile or not.
Does something like this exist, or what is usually being done here? Working only with sldd can't be the solution, as it is not dynamic enough (e.g. multiple different parameter sets for the same model depending on current environment variables)

Answers (1)

I understand you're trying to automatically update data dictionaries (.sldd) from MATLAB scripts before compilation, while maintaining modularity and avoiding unnecessary recompilations.
Assuming you're working on Windows latest version, I believe:
1.) The issue with InitFcn:
From the documentation: Model Callbacks
"If everything is up to date and the Rebuild configuration parameter is set to If changes in known dependencies detected, then the referenced model does not compile and its InitFcn callbacks do not execute."
I believe this explains why your dictionary updates aren't being picked up.
2.) I would suggest trying:
Use PreLoadFcn callback instead:
The same above attached documentation states:
"PreLoadFcn: Before the model is loaded. Defining a callback code for this parameter is useful for loading variables that the model uses."
  • Set "PreLoadFcn" for each referenced model
  • Have it call your script to update the ".sldd" before the model loads
Use the dictionary API for conditional updates. Please refer to this documentation for more information - Simulink.data.dictionary.Section - Configure data dictionary section - MATLAB
Use "Simulink.data.dictionary.Section" methods to update only when needed:
  • "getEntry" - check current values
  • "assignin" - update values conditionally
This way Simulink's dependency checker only sees actual parameter changes, not script execution.
Relevant documentation:
Hope this helps!

Categories

Products

Release

R2024b

Asked:

on 16 Apr 2026 at 11:01

Answered:

on 21 Apr 2026 at 6:30

Community Treasure Hunt

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

Start Hunting!