0
votes

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?

1
Are you not just using masks / library blocks to embed your sub models? A screenshot showing the general structure of your "main model" would help in making a minimal reproducible example here...Wolfie
Positive, the sub models are in their own .slx-files. I cannot take a screenshot, since this is work related. However, even if run the commands from the callbacks some variables are missing. It seems one type of library block creates a Bus object ("1x1 Bus", default or created by my colleagues?) in the workspace upon opening the .slx model. If I just run the callback functions these structs are not created. It seems like I am forced to accept my fate, unless there is a way of running the model initialization without opening a new simulink window.Smartskaft2
I wasn't asking you to screenshot your proprietary work, but instead create a minimal reproducible example, emphasis on minimal, which demonstrates the way you're currently making your models interact. Simulink is designed to work with subsystems and libraries which could be entire models or systems (in their own slx files), it sounds like you're structuring your model(s) in an inefficient way. However, without a demo it's going to be very hard for anyone to point out the best way to adapt what you've got. You could start in these docsWolfie
@Wolfie My bad, I was too quick with my assumption. I will see what I can do and edit my question.Smartskaft2
Are you saying that the Callbacks of the referenced models are not being executed? If so, that is strange behavior. As per @Wolfie's comments, a minimal example showing that would be useful to see.Phil Goddard

1 Answers

1
votes

This is an "old fashioned" approach to using Simulink.

There are two model methods to deal with this.

  1. 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

  2. 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)