How to initialize constants for a Simulink model from a MATLAB script?
28 次查看(过去 30 天)
显示 更早的评论
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](https://i.stack.imgur.com/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 个评论
stozaki
2020-2-5
How about automatically setting the constant values of these constant blocks with a script?
回答(1 个)
Roshni Garnayak
2020-2-7
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model, Block, and Port Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!