Reputation: 510
Is there a way to run Simulink model callbacks from the MATLAB command window? I have models that initializes paramameters with callbacks such as PreLoadFcn
or InitFcn
. These models are then used in other Simulink models. Let us call these the main models. When I want to simulate these main models, it crashes unless I open the sub models (with the callbacks earlier mentioned) in a new Simulink window. I wish to be able to run the simulations without opening the sub models.
My current approach is basically:
% 1. Check for if the sub models have been started already, using find_systems(...) (omitting details here)
...
% 2. If not loaded, open sub models (only one here)
open('subModel.slx');
% 3. Simulate main model
sim('mainModel.slx');
I would rather use something like
% 1. Check for if the parameter variables needed are , using exist(...) and some relevant variable name (omitting details here)
...
% 2. If not loaded, run sub model callbacks
...
% 3. Simulate main model
sim('mainModel.slx');
My simulation process should become faster and my screen will be cleaner. Any ideas on how to do step 2. above in a neat way?
Upvotes: 0
Views: 2826
Reputation: 2743
You can use the following code to run Simulink model callbacks from MATLAB code.
% Load the system
cs = load_system(model_path);
% Get the callback script
callback = get_param(cs, 'InitFcn');
% Run the callback
eval(callback);
% Run the system
sim(model_path);
% Close the system
close_system(cs)
Upvotes: 0
Reputation: 7006
This is an "old fashioned" approach to using Simulink.
There are two model methods to deal with this.
Data Dictionaries (https://uk.mathworks.com/help/simulink/ug/what-is-a-data-dictionary.html) These store variables, data types, buses etc that may be required by models and can be shared by many models
Simulink Projects (https://www.mathworks.com/discovery/simulink-projects.html) This allows you to store groups of models together within the same project. When you open/close a project, a group of "startup" or "shutdown" functions can be called to configure the environment. Your startup file for your project could contain code to load all the submodels (without having to open them) which would setup your workspace. With the Simulink Project approach, you are best to keep the "PreLoad" callbacks empty and deal with any model configuration through some other means (like startup scripts or data dictionaries)
Upvotes: 1