control simulink from M-file

后端 未结 3 2022
清酒与你
清酒与你 2021-01-07 06:22

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

相关标签:
3条回答
  • 2021-01-07 06:41

    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.

    0 讨论(0)
  • 2021-01-07 06:52

    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. ?

    0 讨论(0)
  • 2021-01-07 06:52

    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.

    0 讨论(0)
提交回复
热议问题