0

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
Oscar Paz
  • 18,084
  • 3
  • 27
  • 42
Bensa
  • 127
  • 2
  • 13
  • 1
    what language is this? – Oliver Mar 13 '14 at 21:07
  • This is Matlab language. – Bensa Mar 14 '14 at 14:03
  • Have you tried printing timer? – Oliver Mar 14 '14 at 21:40
  • Yes, timer returns 0. Something is not right with my for loop structure. As I increase the range, the delay before the start of simulation increases, so the for loop isn't even active during the simulation and ends before it. How can I define my timer within the for loop so the timer value is updated every cycle? – Bensa Mar 17 '14 at 17:49

2 Answers2

0

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.

Oliver
  • 27,510
  • 9
  • 72
  • 103
0

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
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Jára
  • 1