MATLAB: Creating a moving line to depict progress

barfigurefunctionMATLABmoving lineprogresstime

I want to create a line at the top of a figure that will move like a progress bar that completely fills up. My idea is to make it so that it's a function of time. I have a code for a Whack-A-Mole game and I have it set up so that instead of having a limited amount of time to "Whack" the moles, each whack gets progressively faster. I want some sort of a progress bar that crosses the screen at the same rate (indicating that when the bar has crossed the screen there is no more time left to whack the mole). I haven't seen anything that does this. Would appreciate any help.

Best Answer

Simple and crude but possibly relevant to what you are hoping for?
The actual progress bar is a set of axes. A patch shows how 'filled' it is. Here, it fills at a speed of 1 bar per 15 seconds. Make it faster by decreasing secAllowed.
playGame = true;
f = figure;
a = axes(f,...
'Position', [0.1 0.95 0.8 0.05],...
'YColor', 'none',...
'XColor', 'none',...
'NextPlot', 'add',...
'XLim', [0 1],...
'XLimMode', 'manual');
t = a.XLim(1);
p = patch([0 t t 0], repelem(a.YLim,1,2), 'r',...
'EdgeColor', 'none');
secAllowed = 15;
t0 = tic;
while playGame
tn = toc(t0);
p.XData = [0 tn/secAllowed tn/secAllowed 0];
drawnow;
if tn>=secAllowed
playGame=false;
end
end