How can i implement a timed counter in app designer
32 次查看(过去 30 天)
显示 更早的评论
I really need to make use of a counter that will increase every 0.25 seconds in app designer ,I've used the timer function in a matlab script but I'm not sure how to go about implementing this in app designer, i want the count variable to increase after every 25 milliseconds as long as the toggle switch is in a certain position . Please help from the beginning, im not familiar with app designer whatsoever
0 个评论
采纳的回答
Geoff Hayes
2021-12-21
@Keenan Jogiah - you could create a timer object within the app that you would then start and stop based on the toggle. In this example, I'm assuming that the toggle is a checkbox - when checked, the timer will start and update the text area with a value that will increase by one over time. When the checkbox is unchecked, the timer will be stopped. I needed to add the following code to do this
properties (Access = private)
mTimer % timer
mCounter; % integer counter
end
which are the data members for the timer object and counter. The callback for the checkbox does "all" of the work
% Callbacks that handle component events
methods (Access = private)
% Value changed function: CheckBox
function CheckBoxValueChanged(app, event)
value = app.CheckBox.Value;
if value == 1
% checkbox is checked so reset counter, create and start
% timer
app.mCounter = 0;
app.mTimer = timer('TimerFcn', @timerCallback, 'ExecutionMode', 'FixedRate', 'Period', 0.25);
start(app.mTimer);
else
% checkbox is unchecked so stop timer
stop(app.mTimer);
end
% this is the callback for the timer
function timerCallback(~, ~)
% update the text area and increment the timer
app.TextArea.Value = num2str(app.mCounter);
app.mCounter = app.mCounter + 1;
end
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 C Shared Library Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!