How to programmatically determine if in headless mode..?

I'm working on an issue with the Parallel Computing Toolbox (PCT)..
As each MATLAB.exe worker thread process is initialized, startup.m is executed. All worker threads are "headless" (no output window). Certain graphics-related function calls that are fine-- when launched within the full MATLAB app with its output capability-- can cause problems in the headless context.
I have a graphics-related function that's very convenient to have in the full MATLAB app's startup.m, but mustn't execute in the PCT worker thread context. Is there a function call I can insert into startup.m to determine if its MATLAB.exe process is running in headless mode? I would like to use as follows:
% Is there an output window associated with this MATLAB.exe process?
if ~isInHeadlessMode()
% YES : render graphics window
someGraphicsFunction();
end
Any help appreciated, Thanks, Brad

 採用された回答

Edric Ellis
Edric Ellis 2012 年 1 月 16 日

3 投票

One option is to check:
java.lang.System.getProperty( 'java.awt.headless' )

4 件のコメント

Bradley Stiritz
Bradley Stiritz 2012 年 1 月 17 日
Hi Edric, thanks for your suggestion. Your Java knowledge is very impressive! I would have accepted your answer, but someone at Mathworks suggested an even simpler function call..
Thank you again for posting,
Brad
Edric Ellis
Edric Ellis 2012 年 1 月 18 日
FWIW, "usejava('desktop')" is fine for most purposes, but it's not quite the same thing - for example, if you run MATLAB in "-nodesktop" mode (as I do most of the time), then it will give you the wrong answer - you can still produce graphics in "-nodesktop" mode. Oh, and I'm a MathWorker too ;)
Bradley Stiritz
Bradley Stiritz 2012 年 1 月 23 日
Thanks for the deeper understanding, very appreciated. Sorry to have judged your suggestion too superficially. Accepting now..
Seth Hillery
Seth Hillery 2025 年 5 月 13 日
unfortunately this only works for traditional desktop environments. Is there any way to also detect say a matlab online/matlab-proxy-app browser based environment?

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

その他の回答 (1 件)

Bradley Stiritz
Bradley Stiritz 2012 年 1 月 17 日

1 投票

Per Nathan Lloyd in Mathworks PCT support, the native MATLAB usejava() function can be used to determine whether the executing MATLAB.exe instance is in 'desktop' or 'headless' mode..
"[The following] code checks to see if a desktop exists before executing the sensitive block of code. For any worker, the desktop will never exist, so the code will be jumped over and the workers will persist; while the client MATLAB will execute the 'startup.m' file without issues."
if usejava('desktop')
% YES : render graphics window
someGraphicsFunction();
end

カテゴリ

ヘルプ センター および 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