Using Timer in startup.m
2 views (last 30 days)
Show older comments
Hello,
is it expected behavior that it is not possible to use timer objects in the startup.m? I have seen that in case I include a call to a script in startup.m, where the script creates and starts a timer, obviously the TimerFcn callback is never executed. But there is also no error when the timer is created or started. On the other hand, when I use the -r command line option and pass the name of the script, the timer works as expected. I haven't found any hint in the documentation about this issue. I'm using R2010b 64bit on Linux.
Regards,
Christian
0 Comments
Answers (5)
Daniel Shub
on 21 Jun 2012
It works for me in Linux. From the command line (note that this will overwrite the startup.m file in the current directory)
$ echo "start(timer('TimerFcn', 'why', 'Period', 1.0, 'ExecutionMode', 'fixedDelay'));" > startup.m
$ matlab
After a few seconds I have:
The bald and not excessively bald and not excessively smart hamster obeyed a terrified and not excessively terrified hamster.
To fool the tall good and smart system manager.
The rich rich and tall and good system manager suggested it.
He wanted it that way.
The programmer suggested it.
Barney suggested it.
To please a very terrified and smart and tall engineer.
The tall system manager obeyed some engineer.
To satisfy some programmer.
Damian wanted it that way.
Can you rephrase that?
EDIT
The above looks like it works, but it doesn't really. A slight modification shows the failure:
$ echo "start(timer('TimerFcn', 'why', 'Period', 1.0, 'ExecutionMode', 'fixedDelay')); disp('Will the timer fire during a 10 second pause?'); pause(10); disp('Now the pause is over?');" > startup.m
This creates the following startup.m
start(timer('TimerFcn', 'why', 'Period', 1.0, 'ExecutionMode', 'fixedDelay'));
disp('Will the timer fire during a 10 second pause?');
pause(10);
disp('Now the pause is over?');
There should be answers to "why" during the pause, but there are not. It appears that timer objects do not execute until after startup.m (more likely matlabrc.m) exits.
I am not aware of this being documented and would guess it is a bug.
0 Comments
Malcolm Lidierth
on 22 Jun 2012
STARTUP.m called from MATLABRC.m MATLAB during startup. Any code specified with -r gets called after startup. Not all commands that are valid after startup can be called from startup.m.
E,g,
props=java.lang.System.getProperties();
props.put(.....)
works, but
java.lang.System.getProperties().put(....)
does not giving
Static method or constructor invocations cannot be indexed.
Do not follow the call to the static method or constructor with
any additional indexing or dot references.
3 Comments
Malcolm Lidierth
on 22 Jun 2012
@Daniel
I would not call it a bug. That startup.m runs "during" startup is documented so it seems reasonable to expect the system not to be fully loaded when it runs.
Daniel Shub
on 22 Jun 2012
It seems like we are just left to guess what functions are allowed in startup.m and matlabrc.m.
Christian
on 21 Jun 2012
1 Comment
Daniel Shub
on 21 Jun 2012
This really should be a comment to my answer and /or an edit to the question instead of a new answer.. If my answer was helpful, you should consider voting for it. Some code would help figure out what you are doing wrong. Are you sure startup.m is being run?
Christian
on 21 Jun 2012
2 Comments
Daniel Shub
on 21 Jun 2012
It looks like a bug to me. See the edit to my answer. I can think of two easy work arounds: use the -r option or have the first callback of the timer launch do_some_calculations.
Jan
on 21 Jun 2012
TIMER requires Java. Although the documentation states, that -nodekstop does start Java, it is worth to check this by javachk('jvm').
3 Comments
Jan
on 22 Jun 2012
I agree that -nodesktop is not the reason. I suggested to check, if Java is available in both cases.
Daniel Shub
on 22 Jun 2012
Adding javachk('jvm') to startup.m does not cause problems. However, if MATLAB and Java are not fully initialized, it is not clear if JAVACHK actually works correctly. Running javachk('jvm') after starting up with the -nodesktop option, does not produce an error message.
See Also
Categories
Find more on Startup and Shutdown 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!