How can i implement a timed counter in app designer
74 views (last 30 days)
Show older comments
Keenan Jogiah
on 21 Dec 2021
Commented: Keenan Jogiah
on 21 Dec 2021
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 Comments
Accepted Answer
Geoff Hayes
on 21 Dec 2021
@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
More Answers (0)
See Also
Categories
Find more on Create Custom UI Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!