2
votes

I am trying to control a simulink from a M-file. What I want to do in the M-file is give the simulink model some input, run the simulink model, change one input value at 0.6 seconds, continue running the simulink model with the new input.

I already know that by using set_param, I can start, pause and continue the simulink, but the problem is I don't know how to pause the simulink model at a certain time(0.6s), is it possible to get the current time from simulink model and read it in the M-file?

Another way I already know is using sim to run simulink model from 0 to 0.6s, and use SimState to save the information at 0.6s, then load these information to resume the simulation. I am trying to change the input before the simulation resumed, but it seems that the model will load the input values from the information it saved, it won't take the new input value.

I stuck in this problem for a very long time, could someone help me with this please?

Thank you very much.

3

3 Answers

1
votes

This is not currently possible from an M-file. If you want to dynamically change the input at a given time externally, it will require an S-Function. Even this solution is difficult and wrought with flakey-ness since the Mathworks does not want to support this functionality in that it defeats one of the features of another toolbox they sell. In time, I believe they will grant this privledge, but it does not exist today. Also, why not use a dynamic input block to change the input value, like a map, signal builder, etc. ?

1
votes

You can get the current time of a running simulation with:

get_param('simulink_model_name', 'SimulationTime');

So for instance by checking this value from your M-file during simulation by using

timer(...)

you can detect when the simulation is at 0.6 seconds.

1
votes

I used a combination of simulink and m-script to achieve a similar goal.

In your model, add one 'assert' block. Double click it, and uncheck 'Stop Simulation when assertion fails'. In the 'Simulation Callback when assertion fails' field, add three commands:

  1. set_param(bdroot,'SimulationCommand','pause');
  2. run('myscript.m'); %insert the script name
  3. set_param(bdroot,'SimulationCommand','continue');

Now connect the inport of this block to a 'not equal to' relational operator. Connect the first inport of the relational operator to a clock (pls set the decimation for analog clock or the sample time [usually -1 for inherited] for the digital clock). The second inport is connected to constant block with a value of 0.6

On simulating the model, the simulation will pause at 0.6 sec, execute the m-file to change the input parameter (considering that it's tunable) and then continue with the simulation.

The assertion block is called when its input signal becomes 0. At 0.6 sec, the output of the relational operator will be 0.

Let me know if it worked.