简体   繁体   中英

Execute a function using simulation time

This might be a very simple problem, but I want to use the simulation time in order to execute a function while the simulation is running. For example, when the simulation time is larger than 14 secs (or >=14 & <20 s) , then change the values of parameters. Please help me, the following code doesn't do anything. Thanks.

a=0;
set_param('model','SimulationCommand','Start');
b=0;

for i=1:1:500000 % 1ms sampling rate
    timer(:,1)=get_param('model','SimulationTime');
    if timer>=14
        a=1;    
        b=1;
    end
end

I don't use matlab, but one thing I notice is that the script does the loop and never yields. This to me would indicate that the script will run in one go, rather than get called repeatedly at every frame. Assuming this script gets executed once, I presume you would register a callback to be executed every time step, see TimerFcn . In this callback you check the timer and take action accordingly. There is also Timer Callback Functions , but I couldn't tell if it was relevant, as the code there is quite different from what you have.

Yielding you can solve with pause() command.

I see only way to export Clock block to Matlab variable. And then use your variable to check.

Anyway, your approach seems to be workable:

>> load_system('MySystem.mdl');
>> get_param('MySystem','SimulationTime')
ans =
     1
>> set_param('ItFixBody', 'SimulationCommand', 'Start');
>> get_param('ItFixBody','SimulationTime')
ans =
    2.4800
>> get_param('ItFixBody','SimulationTime')
ans =
    3.0500
>> get_param('ItFixBody','SimulationTime')
ans =
    3.5800
>> get_param('ItFixBody','SimulationTime')
ans =
    3.9800

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM