Compatibility: Difference between revisions

91 bytes added ,  19 April 2012
m
→‎Are we running octave?: use code template
(→‎Are we running octave?: why are we using persistent)
m (→‎Are we running octave?: use code template)
Line 7: Line 7:
The most commonly used trick is to create a subfunction that will check whether we are running octave, and use it as condition to run selected block of code that are incompatible. Due to the persistent variable it can be called repeatedly without a heavy performance hit.
The most commonly used trick is to create a subfunction that will check whether we are running octave, and use it as condition to run selected block of code that are incompatible. Due to the persistent variable it can be called repeatedly without a heavy performance hit.


{{Code|use and creation of is_octave()|<syntaxhighlight lang="matlab">
  function foo
  function foo
   ## fancy code that works in both
   %% fancy code that works in both
   if (is_octave)
   if (is_octave)
     ## use octave super_powers
     %% use octave super_powers
   else
   else
     ## do it matlab way
     %% do it matlab way
   end
   end
   ## fancy code that works in both
   %% fancy code that works in both
  end
  end
   
   
  ## subfunction that checks if we are in octave
  %% subfunction that checks if we are in octave
  function r = is_octave ()
  function r = is_octave ()
   persistent x;
   persistent x;
Line 25: Line 26:
   r = x;
   r = x;
   end
   end
  end
  end</syntaxhighlight>}}