MATLAB: Does Simulink.sdi.save save incomplete data to a MAT file when starting a simulation by setting the “SimulationCommand” parameter to ‘start’

simulink

I am attempting to save Simulation Data Inspector (SDI) data onto a MAT file programmatically. I use the following command to start simulation using a script:
 
set_param(gcs,'SimulationCommand','start');
In subsequent lines of the script, the run objects are generated, and the signals plotted in SDI are saved using:
 
Simulink.sdi.save('<filename>.mat');
This saves incomplete, or no signals on the MAT-file. However, when I save the plots from the GUI of the SDI, it works well. What could be the issue while saving programmatically?

Best Answer

The issue with the script is the way the simulation is run, i.e:
 
set_param(gcs,'SimulationCommand','start');
This command starts the simulation. but does not guarantee that the simulation ends before progressing to the subsequent part of the script which writes and saves to SDI. What happens is that the script finishes saving the incomplete data to a MAT file when simulation is still writing to SDI.
As a workaround, you may use the "sim" command to run the simulation. This guarantees that the simulation finishes before progressing to the next line of script.