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.
Best Answer