In app designer, I must start timer in pushbutton callback rather than preferred startupFcn callback.

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

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.
I added a pause(1) and then pause(10) before the start command with the same error message. When I queried an AI, it suggested this same fix. The only code in a stripped down startupFcn is the initialization of the timer, a delay, and then the start command. Only if the start command is relocated to a pushbutton callback does it work.
Oh, I have the problem on R2022a. I can try other revisions.
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.
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."
Reasonable suppostition, @Steven Lord. I wondered about that but (perhaps unwarranted) made the presumption the callback would have had the same issue either way.
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.
I had actually noticed that in a quick Recent Activity browse purely by serendipity... :>)

Sign in to comment.

 Accepted Answer

I contacted Mathworks and Lili helped me out. The trick is to include the parameter 'StartDelay" in the timer instantiation. I had to set it to 10 seconds to allow for a very slow read of my .mlapp script. Holding off the timer starting allows all the disk drive work to complete. Now there is no button needed to start the timer. Case closed.

More Answers (1)

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

Products

Release

R2022a

Asked:

Ken
on 30 Jul 2026 at 0:31

Answered:

Ken
on 20 Aug 2026 at 21:35

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!