How do I detect if I'm running MATLAB or Octave?

MatlabPortabilityOctave

Matlab Problem Overview


I need to write code that should run equally well in Octave and on MATLAB. Problem is that it needs to do some GUI stuff, which MATLAB and Octave handle completely differently.

Is there a way I can detect if I'm running MATLAB or Octave, in order to call the right function?

Matlab Solutions


Solution 1 - Matlab

You could use the following test to differentiate Octave from MATLAB:

isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;

Solution 2 - Matlab

There is also a [hint][1] in the wiki on the official octave.org website. They propose the following:

Edit: Not all versions of Matlab support '#' for comments so I changed the example to use '%' instead. It works in Matlab R2018 (Linux) and Octave 4.2.2

function foo
  %% fancy code that works in both
  if (is_octave)
    %% use octave super_powers
  else
    %% do it matlab way
  end
  %% fancy code that works in both
end

%% subfunction that checks if we are in octave
function r = is_octave ()
  persistent x;
  if (isempty (x))
    x = exist ('OCTAVE_VERSION', 'builtin');
  end
  r = x;
end

[1]: http://octave.org/wiki/index.php?title=Compatibility "Compatibility - Octave"

Solution 3 - Matlab

I would use, for example, the ver command, which yields:

in MATLAB:

MATLAB Version 7.7.0.471 (R2008b) Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686 Java VM Version: Java 1.6.0_04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

in Octave:

GNU Octave Version 3.0.5 GNU Octave License: GNU General Public License Operating System: Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686

Another possibility is to use the license function.

Solution 4 - Matlab

In Matlab:

>> exist __octave_config_info__
ans =
     0

In Octave:

octave:3> exist __octave_config_info__
ans =  5

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlindelofView Question on Stackoverflow
Solution 1 - MatlabAmroView Answer on Stackoverflow
Solution 2 - MatlabBernhardt RoggeView Answer on Stackoverflow
Solution 3 - MatlabjmbrView Answer on Stackoverflow
Solution 4 - MatlabPeterView Answer on Stackoverflow