In app designer, I must start timer in pushbutton callback rather than preferred startupFcn callback.
Show older comments
I intitialize the timer in startupFcn. Then I start the timer with: app.PollTimer.start It starts running for about 16 times and then throws the error:
"Error while evaluating TimerFcn for timer 'timer-2'.
Trying to load file " . . . .mlapp while it is being loaded."
If I create a push button callback with the single line: app.PollTimer.start then the timer runs perfectly. Adding this push button is pretty Micky Mouse when it seems more appropriate to start the timer automatically in startupFcn. So the question is: How to start the timer automatically when the application starts?
8 Comments
dpb
on 30 Jul 2026 at 1:18
We can't see your starup function, but if the timer start is the very last thing in the function, this should work. Which release or MATLAB are you using?
You could try adding a short delay before initializing, but it shouldn't be necessary.
Ken
on 30 Jul 2026 at 2:39
Ken
on 30 Jul 2026 at 2:40
dpb
on 30 Jul 2026 at 13:22
OK, I was wondering if it was R2025 or later might have something to do with the symptoms.
The other idea might be to put the timer in the pushbutton callback, but then call its callback in the startup function instead of manually pressing it. That still shouldn't be necessary I think, but might uncover something about the startup sequence.
I would suggest this is worthy of submitting to Mathworks as an official support request/bug at <Product Support Page>. Sometimes a MathWorks staffer will see some of these kinds of issues and comment here, but its not ever guaranteed.
Steven Lord
on 30 Jul 2026 at 13:46
The error message indicates it's the TimerFcn for your timer that's throwing the error. Without seeing what your TimerFcn is doing and in particular how it's trying to interact with the app it's probably going to be difficult to offer any concrete guidance.
From the part of the text of the error message you posted I wonder if you're trying to (or are inadvertently doing it even if you don't intend to) launch the app from within the TimerFcn. That feels like it could cause problems, either related to recursion or related to using an app that's not fully initialized (a "Ready, Fire, Aim!" type problem.)
"Error while evaluating TimerFcn for timer 'timer-2'.
Trying to load file " . . . .mlapp while it is being loaded."
dpb
on 30 Jul 2026 at 14:01
Reasonable suppostition, @Steven Lord. I wondered about that but (perhaps unwarranted) made the presumption the callback would have had the same issue either way.
Steven Lord
on 19 Aug 2026 at 20:32
FYI the poster created a new Answers post with this question this afternoon. I closed it as a duplicate of this one and asked them to continue the conversation here.
dpb
on 19 Aug 2026 at 21:28
I had actually noticed that in a quick Recent Activity browse purely by serendipity... :>)
Accepted Answer
More Answers (1)
dpb
on 20 Aug 2026 at 20:48
I built a MWE that illustrated starting a repetitive timer in the startup() function that runs 100 iterations before it kills itself. All it does is display the callback time event data in a text field ("FINISHED" when the task count matches the target with a 5 sec pause before it kills itself).
I attached the .mlapp file of the full app but the startup and update callback functions are below.
The code was basically stolen from the timer doc examples although there's a problem with the structure there in that the first example won't work as given because must have a TimerFcn callback defined even if only start/stop events occur. Took a while to figure out what was wrong there.
properties (Access = private)
hTimer % handle to timer
end
methods (Access = private)
function results = startup(app)
t= timer;
t.TimerFcn = @(src,thisEvent)updatetimerdisplay(app,thisEvent);
t.Period = 0.5;
t.TasksToExecute = 100;
t.ExecutionMode = 'fixedRate';
app.hTimer = t;
start(app.hTimer)
end
function updatetimerdisplay(app,event)
app.TimerCountTextArea.Value=(datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF'));
%disp(datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF'))
if app.hTimer.TasksExecuted==app.hTimer.TasksToExecute
app.TimerCountTextArea.Value='FINISHED';
pause(2)
delete(app.hTimer)
delete(app)
end
end
end
Categories
Find more on Environment and Settings 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!