添加私有函数
properties (Access = private)
Timer_id; % 定时器id
end
properties (Access = public)
speed_1;
end
methods (Access = private)
% 定时器初始化,重复模式
function timer_init(app)
app.Timer_id = timer;
app.Timer_id.StartDelay = 0.01; % 开启的延时生效时间
app.Timer_id.Period = 1.0;% 周期
app.Timer_id.ExecutionMode = 'fixedSpacing';
app.Timer_id.TimerFcn = @(~, ~) timer_handler(app);
end
% 定时器启动
function timer_start(app)
start(app.Timer_id);
end
% 定时停止
function timer_stop(app)
stop(app.Timer_id);
end
% 删除定时器
function timer_delete(app)
delete(app.Timer_id);
end
%定时器回调
function timer_handler(app)
%执行定时器任务
% app.EditField.Value = app.EditField.Value +1;
%app.speedGauge.Value=evalin("base",'test1');
%app.speedGauge.Value=app.speed_1.OutputPort(1).data;
%app.Label.Text= sprintf('%s',app.speed_1.OutputPort(1).data);
app.speedGauge.Value=app.speedGauge.Value+1;
end
end
右键按钮选择添加回调函数输入一下代码,读取按钮状态,初始显示“开始”,点击显示“停止”并开始定时。
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Button
function ButtonValueChanged(app, event)
if app.Button.Value ==1
app.Button.Text='停止';
timer_init(app);
timer_stop(app);
app.Timer_id.Period = 0.5;
timer_start(app);
else
app.Button.Text='开始';
timer_stop(app);
end
end