superweizen
superweizen

Reputation: 11

Matlab and Simulink simulation using set_param and get_param

I am using the command set_param and get_param to control a very simple simulink model, which only consists of a clock block and a scope. What I want is to let the simulink model run, get the real-tiem output of the clock block from Simulink to Matlab and stop the simulation when the clock value is greater than 5. The following is my Matlab script:

Simulink_Model='co_simulation_sl';                      
load_system(Simulink_Model);

open(Simulink_Model);
time=0;

while time<5

if strcmp (get_param(Simulink_Model,'SimulationStatus'), 'stopped')
      set_param(Simulink_Model,'SimulationCommand','start')
   elseif strcmp (get_param(Simulink_Model,'SimulationStatus'), 'paused')
      set_param(Simulink_Model,'SimulationCommand','continue')
   end
   set_param(Simulink_Model,'SimulationCommand','pause')
   block = 'co_simulation_sl/Clock';
   rto = get_param(block, 'RuntimeObject');
   time = rto.OutputPort(1).Data;
end
set_param(Simulink_Model,'SimulationCommand','stop')

enter image description is here.

However the while loop cannot run itself correctly. I can only observe the change of the variable "time" when I manually pause and continue the Simulink model. I'm using a fixed simulation time step of 0.1s.

I have already searched a lot, but still cannot find any reason. Could you help me? Thank you in advance.

Upvotes: 1

Views: 1649

Answers (1)

Kobaてつろー
Kobaてつろー

Reputation: 1

I wanted to set a parameter at a specified time, so I paused it with the Asert block and then changed the parameter with the set_param command. I was able to achieve this by combining pause(0.1) and Asert in your code. Thanks a lot! https://jp.mathworks.com/help/simulink/ug/controlling-execution-of-a-simulation.html

Simulink_Model='co_simulation_sl';
load_system(Simulink_Model);

open(Simulink_Model);
time=0;

while time<5

if strcmp (get_param(Simulink_Model,'SimulationStatus'), 'stopped')
      set_param(Simulink_Model,'SimulationCommand','start')
   elseif strcmp (get_param(Simulink_Model,'SimulationStatus'), 'paused')
      set_param(Simulink_Model,'PauseTime','Value','XX');Here is add code
      set_param(Simulink_Model,'SimulationCommand','continue')
   end
   %set_param(Simulink_Model,'SimulationCommand','pause')
   pause(0.1);Here is add code
   block = 'co_simulation_sl/Clock';
   rto = get_param(block, 'RuntimeObject');
   time = rto.OutputPort(1).Data;
end
set_param(Simulink_Model,'SimulationCommand','stop')

Upvotes: 0

Related Questions