How to initialize constants for a Simulink model from a MATLAB script?
47 views (last 30 days)
Show older comments
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.
- 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 Comments
stozaki
on 5 Feb 2020
How about automatically setting the constant values of these constant blocks with a script?
Answers (1)
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:
0 Comments
See Also
Categories
Find more on Naming Conventions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!