How to initialize constants for a Simulink model from a MATLAB script?
36 views (last 30 days)
building a Simulink model with Matlab Function blocks. These function blocks have a lot of constants, for example g=9.8. I want to initialize all of these constants in one go in a Matlab script, so that I don't have to do so in each function block.
Here's what I have tried until now:
- In the Matlab Function block I have initialized the variables using a Constant block, which is given as a input to the function block. This system works, but there are a lot of constant blocks in the model and it's getting very clustered.
- I have also tried declaring these variables as global variables in the Matlab script. This does not work.
- Another way that I have tried is so to create a function script for these constants and then load this function script in the Matlab Function block. This does not work.
Is there a way that I can just initialize these values from the Matlab script and the Simulink model reads it from the Matlab script, without me having to use these constant blocks?
constantblocks(intializationvalues) -> matlab function block -> output
What's the best way to solve this problem?
Roshni Garnayak on 7 Feb 2020
You can use the callbacks available in the Model Properties for initializing constants. The callbacksthat you can use depending on your requirement are PreLoadFcn, PostLoadFcn and InitFcn.
Refer to the following link to know the procedure to create model callbacks and the details about the execution of different callbacks:
You can also refer to the following link to get an idea on how to load variables when opening a model: