MATLAB Answers

Is it possible to programmatically check whether MATLAB has been started with the "-nodisplay" option?

조회 수: 46(최근 30일)
I have a program that needs to behave differently depending on whether MATLAB has a display or not. However, I cannot determine a way to programmatically check this.
The reason I need to do this is that I sometimes start MATLAB in batch mode from a shell script, for testing my programs.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2013년 10월 18일
The ability to programmatically check whether MATLAB has a display is not available in MATLAB 7.6 (R2008a).
As a workaround, you can do one of the following:
1. Manually set or unset an environment variable in your shell script that launches MATLAB, so you can query it from inside MATLAB. For instance, setting the ISDISPLAY environment variable in your shell script before launching MATLAB:
setenv ISDISPLAY no
matlab -r "foo; quit"
(here shown with C Shell syntax), means that the MATLAB command
will return the string 'no' in any MATLAB processes that are spawned from this shell.
2. Query the "ScreenSize" property of the root object inside MATLAB:
get(0, 'ScreenSize')
When there is no display, this returns [1 1 1 1] instead of an actual screen size. 
Note that this will only work in releases prior to R2014b.

추가 답변(3개)

Paul Macey
Paul Macey 2016년 1월 26일
You may want usejava:
This returns false for an instance of MATLAB started with -nodisplay option. I used this for a subroutine using the parallel processing toolbox, where I called a function in a parfor loop that had an error check with a msgbox and uiwait.

Jasper 2015년 8월 6일
편집: MathWorks Support Team 2021년 6월 24일
get(0, 'ScreenSize') % does not work in Matlab 2015a
On Matlab 2015a this will give just normal screen coordinates even when I called Matlab with the -nodisplay option.
Instead, I found a good stackoverflow topic on this question:
if usejava('jvm') && ~feature('ShowFigureWindows')
%# use text-based alternative (input)
%# use GUI dialogs (questdlg)



Community Treasure Hunt

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

Start Hunting!

Translated by