Increment a variable by Simulink

落花浮王杯 提交于 2019-12-08 07:22:36

问题


I have a problem in Simulink, I have a variable "k" as constant Block (start Value k =1 ) and i want to increment "k" after each time I click on "the simulation button" untill "k" is 4 then it will be reset to 1 again.

i already try this (see atached Image 1), but in this case k it wil be so long inkremented until the Simulation time is finished (see atached Image 2) and that is not what i want.

enter image description here enter image description here

i will apreciate any Help many thanks Jay


回答1:


If you just want to update the value every time you run the simulation, your best option would be to put some code in the InitFcn callback.

This is a (optional) block of code which is run every time the model is initialized. To do this navigate File > Model Properties > Model Properties

Select the Callbacks tab, and then the InitFcn callback on the left. The following code will check if k exists yet in the workspace, and set it if not, and increment it if so. If you put it in the callback, and then set the constant block value to k you should get the behavior you want.

if ~exist('k', 'var')
    k = 1;
else
    k = k + 1;
end

if k>4
    k = 1;
end


来源:https://stackoverflow.com/questions/39178997/increment-a-variable-by-simulink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!