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

感情迁移 提交于 2019-11-27 04:35:06

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

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

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

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.

In Matlab:

>> exist octave_config_info
ans =
     0

In Octave:

octave:3> exist octave_config_info
ans =  5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!