Matlab / GNU Octave "isinteractive" function

It’s useful to know if the Matlab or GNU Octave GUI is open for a number of use cases, including

  • pause for each group of a large set of plots–only if user is there to look at them, otherwise save to disk and close thereafter.
  • increase (or decrease) verbosity of print statements or if console output is logged, depending on if it batch mode or not.

We don’t use the Matlab ≥ R2019a batchStartupOptionUsed as it doesn’t detect the -nodesktop case often used for unattended batch processing. Save this code to isinteractive.m for your project.

function isinter = isinteractive()
 %% tell if the program is being run interactively or not.
 % helpful to say pause after making groups of plots--only if user has GUI desktop open.
 % don't use batchStartupOptionUsed as it neglects the "-nodesktop" case
persistent inter;

if isempty(inter)
    if isoctave
      inter = isguirunning;
    else % matlab, this test below doesn't work for Octave
	  inter = usejava('desktop');

isinter=inter; % has to be a separate line/variable for matlab