Simulink const block referencing changing variable doesn't update during simulation

In my Simulink diagram, I have a const block that references a global variable (lets call it myVar). I have an event handler that is called every clock tick (looks for a change in a clock block's output). That handler looks like this:
function speedRampUpUTManager(block, eventdata)
simTime = block.OutputPort(1).Data;
global myVar;
if(simTime == 10)
myVar = 1500; //myVar is initialized to 1700
end
disp('myVar:');
disp(myVar);
end
Sure enough, during a simulation the disp shows 1700, then starts showing 1500. However, when I look at the scope attached to the const block with the Simulation Data Inspector, the value is 1700 the whole time.
My const block has the following settings:
  • Const Value: myVar
  • Interpret vector parameters as 1-D: unchecked
  • Sampling mode: Sample Based
  • Sample Time: -1
Perhaps one workaround might be:
set_param(constBlock, 'Value', 'myVar')
But I'd like to avoid that if possible.
Regards, Istvan.

 採用された回答

C.J. Harris
C.J. Harris 2014 年 7 月 7 日

1 投票

Unfortunately unless you call 'set_param' the value of the variable will not be re-evaluated. Therefore calling 'set_param' is the way to do this. I would however recommend you find a way of doing this in Simulink instead, either via switches, or Stateflow if necessary, thereby avoiding the need for an event handler altogether.

3 件のコメント

Istvan
Istvan 2014 年 7 月 7 日
Thanks for the help! Unfortunately, the value still doesn't seem to update until after the simulation is over. If I set the value to '100' then at the end of the simulation the value is still 1700 (the initial value). However, during the entirety of the next simulation, the value is 100. Is that a mistake of mine, or a limitaion of Matlab?
I can't get around doing this with an event handler as the parameter is changed by a unittest case running in matlab.
I've not actually tried this, so I'm just speculating here, by maybe it is necessary to also call the 'update' method:
set_param(bdroot,'SimulationCommand','update')
Istvan
Istvan 2014 年 7 月 7 日
Unfortunately, that didn't work. Since this isn't related to the initial question, so I posted a new one here .

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEvent Functions についてさらに検索

質問済み:

2014 年 7 月 7 日

コメント済み:

2014 年 7 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by