Using Timer in startup.m

9 ビュー (過去 30 日間)
Christian
Christian 2012 年 6 月 21 日
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

回答 (5 件)

Daniel Shub
Daniel Shub 2012 年 6 月 21 日
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.

Malcolm Lidierth
Malcolm Lidierth 2012 年 6 月 22 日
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 件のコメント
Malcolm Lidierth
Malcolm Lidierth 2012 年 6 月 22 日
@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
Daniel Shub 2012 年 6 月 22 日
It seems like we are just left to guess what functions are allowed in startup.m and matlabrc.m.

サインインしてコメントする。


Christian
Christian 2012 年 6 月 21 日
Daniel, I can confirm that your example works on my system as well, the timer is running as expected. So I am a bit confused now. My setup is a bit more complicated than this example but I think I have reduced it as much as possible and it shows the behavior as described above: I have a perl script that calls another perl script. The second script creates the startup.m and runs Matlab afterwards. As described, when using startup.m, the timer does not start. But when I change the name of the m-file that is created by the second perl script to something other that startup.m and run matlab with -r, passing this filename, it works. I will try to investigate this further.
  1 件のコメント
Daniel Shub
Daniel Shub 2012 年 6 月 21 日
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
Christian 2012 年 6 月 21 日
Ok, here is some code. I reduced it to the very basics. There is only one perl script left:
#!/usr/local/bin/perl -w
$matlab_call="matlab --version R2010b -nodesktop -nosplash";
open(INPUT,">startup.m");
print INPUT "start(timer('TimerFcn','why','Period',1.0,'ExecutionMode','fixedDelay'));";
print INPUT "do_some_calculation();\n";
print INPUT "exit\n";
close(INPUT);
system("$matlab_call");
system("/bin/rm -f startup.m");
In this setup, it creates startup.m. The function do_some_calculation is simple:
function do_some_calculation( )
for ii = 1:3
ii
pause(2);
end
end
When running this script, the output in Matlab is
ii =
1
ii =
2
ii =
3
When I change the perl script to
#!/usr/local/bin/perl -w
$matlab_call="matlab --version R2010b -nodesktop -nosplash -r my_startup.m";
open(INPUT,">my_startup.m");
print INPUT "start(timer('TimerFcn','why','Period',1.0,'ExecutionMode','fixedDelay'));";
print INPUT "do_some_calculation();\n";
print INPUT "exit\n";
close(INPUT);
system("$matlab_call");
system("/bin/rm -f my_startup.m");
so that it creates my_startup.m instead, the output is:
The bald and not excessively bald and not excessively smart hamster obeyed a terrified and not excessively terrified hamster.
ii =
1
To fool the tall good and smart system manager.
The rich rich and tall and good system manager suggested it.
ii =
2
He wanted it that way.
The programmer suggested it.
ii =
3
Barney suggested it.
To please a very terrified and smart and tall engineer.
I do not understand why there is a difference.
Regards
  2 件のコメント
Daniel Shub
Daniel Shub 2012 年 6 月 21 日
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.
Christian
Christian 2012 年 6 月 22 日
Yes, you are right. I am fine with the -r option at the moment.

サインインしてコメントする。


Jan
Jan 2012 年 6 月 21 日
TIMER requires Java. Although the documentation states, that -nodekstop does start Java, it is worth to check this by javachk('jvm').
  3 件のコメント
Jan
Jan 2012 年 6 月 22 日
I agree that -nodesktop is not the reason. I suggested to check, if Java is available in both cases.
Daniel Shub
Daniel Shub 2012 年 6 月 22 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by