通过Simulink递增变量 [英] Increment a variable by Simulink

查看:519
本文介绍了通过Simulink递增变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Simulink中有问题,我有一个变量"k"作为常量块(起始值k = 1),并且我想在每次单击模拟按钮"以后递增"k",直到"k"为4,则将再次重置为1.

我已经尝试过此操作(请参见athat的图像1),但是在这种情况下,k可能需要很长的笔墨才能完成模拟时间(请参见atach的图像2),这不是我想要的.

在此处输入图片描述 在此处输入图片描述

非常感谢杰伊,我将给予任何帮助

解决方案

如果您只是想在每次运行模拟时都更新值,那么最好的选择是在InitFcn回调中放入一些代码.

这是一个(可选)代码块,该代码块在每次初始化模型时运行.为此,请导航File > Model Properties > Model Properties

选择回调"选项卡,然后选择左侧的InitFcn回调.下面的代码将检查k在工作空间中是否存在,如果不存在,请设置它,如果存在则将其递增.如果将其放在回调中,然后将常量块值设置为k,则应该获得所需的行为.

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

if k>4
    k = 1;
end

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

解决方案

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

这篇关于通过Simulink递增变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆