AlbertK
AlbertK

Reputation: 231

How to run simulink simulation and matlab script simultaneously

I'm trying to run Simulink model and at the same time receiving and processing data in a Matlab script, and sending the result into Simulink to change the behavior of the model.

Since the Matlab scripts should keep receiving and processing data, the only way I can figure out is to use a while(1) loop. However, the simulation seemed to be blocked by the while loop. The simulation time is halted when I run the script. As long as I Ctrl+C to end the script, the simulation continued.

Is there any way to run the simulink simulation and the Matlab script(or function) simultaneously while keeping the interaction between them? Just like executing them in different thread.

Upvotes: 2

Views: 5603

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10762

MATLAB (+ toolboxes + Simulink) is fundamentally a single threaded application, so there is no simple way of doing what you want. (In recent years some of the underlying math libraries have become multi-threaded, but that doesn't change the high level picture).

You can do either of the following:

  • have 2 sessions running; one running MATLAB and one Simulink with an appropriate communications channel between the two
  • incorporate your MATLAB code into a Simulink S-Function and have it do its stuff every time Simulink takes a time step.

The latter of these is (arguably) the easiest.

Upvotes: 2

Related Questions