Simulink set_param from callback doesn't take effect until after simulation

6 views (last 30 days)
I have a Simulink model that I am trying to test with unittest and sim(), and need to update several const block's values during a test. To do this, I added a clock to the diagram and attached an event listener to it:
add_exec_event_listener('myModel/Sim Time', 'PostOutputs', UTCallback);
That event listener (UTCallback) should update the value of a const block based on the clock's time (note: it also does several other things which is why this functionality isn't in the simulink diagram). The const block I'm trying to update uses a variable for its value (myVar). The problem is, that update doesn't take effect until after the simulation has run. I inspected the simulation graphs and the value doesn't change during the simulation. Immediately after the unit test completes though, the block in Simulink reflects the change, and the next time I run the unit test the new value is used (but another change during the second run isn't reflected until the third run, etc).
Here is UTCallback:
function UTCallback(block, eventdata)
simTime = block.OutputPort(1).Data;
if(simTime == 10)
updateConst(1234);
end
end
Here are two updateConst functions I have tried:
function updateConst(value)
assignin('base', myVar', value);
set_param('myModel', 'SimulationCommand', 'update')
end
function updateConst(value)
set_param('myModel/constblock',...
'value', '200');
set_param('myModel', 'SimulationCommand', 'update')
end
My const block has the following settings:
  • Const Value: myVar
  • Interpret vector parameters as 1-D: unchecked
  • Sampling mode: Sample Based
  • Sample Time: -1
UPDATE: I discovered the following which might be related. If I put this code inside updateConst:
disp(get_param('myModel','SimulationStatus'));
set_param('myModel', 'SimulationCommand', 'pause');
disp(get_param('myModel','SimulationStatus'));
It prints the following:
running
running
Regards, Istvan.

Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!