Pause Simulink simulation and wait for MATLAB to compute a new input before continuing.

3 views (last 30 days)
I would like to pause Simulink, get the ouput to MATLAB and once it's available, send a new input from MATLAB to Simulink and continue from the same state where it stopped. Do you think this is possible and how would you do it?
Extra info: At every clock cycle of a VHDL simulation in ModelSim, I would like to feed an output from ModelSim into Simulink as an input, and send Simulink's output back to ModelSim as an input, creating a closed loop. I am using MATLAB to estabilish a connection and send data to and from ModelSim.

Answers (1)

Shubham
Shubham on 20 Sep 2024
Edited: Shubham on 20 Sep 2024
Hey Martin,
You can indeed pause your simulation and establish an interaction between Simulink and MATLAB. Consider the following code snippet for pausing the Simulation and extracting the data from desired blocks:
model.set_param('model_name', 'SimulationCommand', 'pause');
output_data = get_param('model_name/BlockName', 'RuntimeObject').OutputPort(1).Data;
Once you have the required data, you may perform necessary computations on it in MATLAB. Finally, you may update the values in the Simulink model and continue with the simulation.
set_param('model_name/BlockName', 'Value', new_input_data);
set_param('model_name', 'SimulationCommand', 'continue');
In order to establish a communication between MATLAB and ModelSim, you may use MATLAB's external interfaces to communicate with ModelSim. For running the simulation for only a single time step, use the following command:
sim("ModelName",paramstruct);
% where paramstruct is a structure with field names that match each parameter name and field values that specify the value to use for each parameter.
You can learn more about the programmatic interactions between MATLAB and Simulink by referring to the following documentation: https://www.mathworks.com/help/simulink/ug/using-the-sim-command.html
I hope this helps!

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!