How can I tell matlab it's running in the background?

조회 수: 9 (최근 30일)
Leo Simon
Leo Simon 2013년 3월 11일
댓글: Leo Simon 2014년 4월 14일
I'd like to be able to run a job either in the background or in the foreground. My program runs fine in the foreground but crashes in the background, presumably because it's not seeing some environment variable. The command that causes the crash is useful only for foreground applications (it relates to screen output) so I could easily avoid this problem if I could flag whether or not the program is running in the foreground. So my question is: does matlab know if it's being run as a foreground or background application, and if yes, what variable would indicate this?
Thanks for any advice
  댓글 수: 2
Jason Ross
Jason Ross 2013년 3월 11일
Can you clarify which operating system you are using?
And say exactly how are you get getting "foreground" and "background", e.g. you hit control-z, use nohup, launch from the command line with switches, use a job scheduler, etc.
Leo Simon
Leo Simon 2013년 3월 31일
Hi Jason, Sorry somehow I missed your response. Sorry for the lack of clarity.
I'm running on linux.
sometimes I run using nohup, other times using the -r flag.
I want to distinguish either case from when I'm running a command from within matlab.
In particular, I have a program that I sometimes run as a batch job, other times from within a matlab session. If the former I want to quit out of it, if the latter I want simply to return to the caller workspace
Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Jason Ross
Jason Ross 2013년 4월 1일
I would run "system(env)" in both modes, put the output to a file, and diff them.
I have a sneaking suspicion that the difference is tied to the DISPLAY environment variable.
  댓글 수: 1
Leo Simon
Leo Simon 2014년 4월 14일
Thanks very much Jason, you're quite right! I don't know why I didn't try your suggestion before now. But it works beautifully, at least for linux, haven't tried it on anything else.
In case anybody else cares about this (doesn't seem that way!) here's some code that does the check
%Checks whether a program is running the foreground or background.
%It's in the foreground iff there's a line in system('env') that says
% DISPLAY=:0
function isBg = bgfg;
isBg = 1;
[~,myEnv] = system('env');
startString=findstr('DISPLAY',myEnv);
for ii=1:numel(startString);
endString=startString(ii)+8;
if strcmp(myEnv(endString:endString+1),':0');
isBg=0;
break;
end;
end;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by