How to initialize constants for a Simulink model from a MATLAB script?

102 vues (au cours des 30 derniers jours)
Aksha Jain
Aksha Jain le 3 Fév 2020
I am
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.
1nqyK.png
  • 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?
%matlabscript
intializingvalues;
sim('filename.slx');
postprocessig;
%simulink model
constantblocks(intializationvalues) -> matlab function block -> output
end
What's the best way to solve this problem?
  2 commentaires
stozaki
stozaki le 5 Fév 2020
How about automatically setting the constant values of these constant blocks with a script?
Aksha Jain
Aksha Jain le 6 Fév 2020
I have already tried that. It doesnt work

Connectez-vous pour commenter.

Réponses (1)

Roshni Garnayak
Roshni Garnayak le 7 Fév 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:

Catégories

En savoir plus sur Model, Block, and Port Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by