MATLAB: How run Simulink model automatically from itself if a condition is met

callbackrunsimulink

I need to run simulink twice automatically only after the first run (this is because I need the data from the first simulation in order to feed the next one – this already works in my model by an InitFCN code).
I tried implementing the next code in StopFCN callback, but obviously it doesn't work because "Termination of 'model_name' is in progress"
%no. runs counter
Number_Runs=Number_Runs+1
%only if number of run times is 2 automatically run simulink
if (Number_Runs - 1)==0
sim(SIMULINK_MODEL_L7); %%HERE IS WHERE I TRIED TO RUN BY CODE
end
I can't come across any solution to implement this.
Hope I explained well.
-Ester

Best Answer

Try it in PostSaveFcn, or have an independent and separate M-script to control your simulation.