How to run 2 simulink models in a sequential manner from .m script in a loop?

Actually, I have 2 simulink models in which the first model's outputs are used as input for the next model. I have used 'to' and 'from' blocks to connect one model's outputs with other. The issue is that since they are run in a loop, the workspace is not getting updated. Due to this thing, the from blocks in the 2nd model cannot read the outputs from the first model. How to overcome this issue? (Please check the attached files)

 Respuesta aceptada

Do the following:
Open your first model, go to File->Model Properties->Model Properties->Callback->StopFcn and type
a=1;
then from command line, type the following:
sim('firstmodel.slx');
if a==1
sim('secondmodel.slx');
end
Hope this helps. Let me know the results.

5 comentarios

Thanks for the quick reply. However, after using your method there are couple of issues. Firstly, in my case, when first model runs, it executes the second model and then the second model executes the first model and so on.
This goes in a cyclic fashion until a specific time is reached. I want to use the outputs at each stage so that the next model can use them. I have attached the Stopfcn code and the model files for your reference. Please check.
I understand that you want to run these systems for one step size of them, which is 0.1 seconds. Have you set the simulation time to also 0.1?
Jasvir Virdi
Jasvir Virdi el 7 de Feb. de 2018
Editada: Jasvir Virdi el 7 de Feb. de 2018
Yes, actually as on now entire simulation time is 0.1 seconds and the step size that I am taking is 0.01 seconds.
Please check the attached image. (Sorry for the Japanese)
Both the simulation run for 0.01 seconds in a sequential manner, then timer is increased by 0.01 seconds and then it is rerun again till 0.1 seconds is reached?
The start and end time is same since I want to run the simulation for one sampling time step only. The timer is updated at the very end when all models are executed.
The main problem right now is that the entire thing gets executed once only.The first model runs, then the second runs for t=0.01 seconds. As soon as t is updated to 0.02 seconds, the first model doesn't execute.
Ok, do the following things:
Set your simulation start time to 0 and end time to 0.1. Since your step size is 0.01, it would be running in a sequential manner. Type the following code(actually not a code, a pseudocode, but adapt it):
Ts=0.01;cnt=0;%cnt is a counter
while true
if cnt==0.1;break;end
%your statements goes here
%if_1 statement
%if_2 statement
cnt=cnt+Ts;
end
The main thing is to make the loop run until cnt is 0.1 and then break the loop. Hope it is clear.
P.S: Run this code from command line, not from StopFcn or any other callback.
Thanks a lot Birdman. I got it working!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Feb. de 2018

Comentada:

el 7 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by