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

后端 未结 4 1143
再見小時候
再見小時候 2020-11-30 08:16

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 different

4条回答
  •  爱一瞬间的悲伤
    2020-11-30 09:03

    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
    

提交回复
热议问题