55

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?

lindelof
  • 34,556
  • 31
  • 99
  • 140

4 Answers4

55

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

isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
Amro
  • 123,847
  • 25
  • 243
  • 454
  • 1
    Good call, though it would be better encapsulated in a function. – Richie Cotton Feb 12 '10 at 09:35
  • Hm. exist('OCTAVE_VERSION', 'builtin') evaluates to 0 on both matlab and octave for me :-( . And I upvoted this answer before checking, whoops. – foobarbecue Apr 21 '22 at 16:32
  • 2
    @foobarbecue works just fine (just tried [it](https://octave-online.net/)), the same test is even included in the official Octave manual: https://octave.org/doc/latest/How-to-Distinguish-Between-Octave-and-Matlab.html – Amro Apr 22 '22 at 06:52
  • Hm, must have been something funky going on with my environment. Works now. – foobarbecue Apr 23 '22 at 23:45
24

There is also a hint 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
Spoonless
  • 561
  • 5
  • 14
Bernhardt Rogge
  • 341
  • 2
  • 6
5

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.

jmbr
  • 3,298
  • 23
  • 23
4

In Matlab:

>> exist __octave_config_info__
ans =
     0

In Octave:

octave:3> exist __octave_config_info__
ans =  5
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
Peter
  • 127,331
  • 53
  • 180
  • 211
  • That's exist('octave_config_info') in an if statement for the Octave syntactically challenged (like me)... – Rhys Ulerich Jul 14 '12 at 19:42
  • 2
    Actually, I get `ans = 0` on **both Matlab and Octave** for this command! – winkmal May 06 '20 at 07:41
  • 1
    @winkmal try the `exist __octave_config_info__` instead. the `octave_config_info` is obsolete. More info [here](https://github.com/octave-app/octave-app/issues/141) – Foad S. Farimani Mar 15 '22 at 08:43