Compatibility: Difference between revisions

From Octave
Jump to navigation Jump to search
(created page)
 
m (small fixes (should use preview more often))
Line 1: Line 1:
Despite best efforts by Mathworks to make Matlab useful, some functionalities still only exist in octave. This page is for tricks that will help in writing code that will run in both octave and matlab.
Despite best efforts by Mathworks to make matlab useful, some functionalities still only exist in octave. This page is for tricks that will help in writing code that will run in both octave and matlab.


Consider the following case. You have found some useful code that is currently matlab specific and want to port it into octave. You may need, or want, to make changes, either due to missing functions in octave or to use simpler/faster/etc... octave specific functions. If you make these changes while keeping it matlab compatibility you can send the modified code, now both matlab and octave compatible, upstream. This will make contributions between the octave and matlab community easier. It will be easier to apply changes/bug fixes that both you and the upstream developer makes.
=Why?=
Consider the following case. You have found some useful code that is currently matlab specific and want to port it into octave. You may need, or want, to make changes, either due to missing functions in octave or to use simpler/faster/etc octave specific functions. If you make these changes while keeping it matlab compatibility you can send the modified code, now both matlab and octave compatible, upstream. This will make contributions between the octave and matlab community easier. It will be easier to apply changes/bug fixes that both you and the upstream developer makes.


 
== Are we running octave? ==
The most commonly used trick is to create a subfunction that will check whether we are in octave or not, and use it as condition to run selected block of code that are incompatible
The most commonly used trick is to create a subfunction that will check whether we are in octave or not, and use it as condition to run selected block of code that are incompatible.


  function foo
  function foo
Line 25: Line 26:
   end
   end
  end
  end
However, the some parts of the code won't work
A good reason to make this is
to be a useful
Often people will want to write code that will work in both octave and matlab but wishing

Revision as of 14:48, 30 November 2011

Despite best efforts by Mathworks to make matlab useful, some functionalities still only exist in octave. This page is for tricks that will help in writing code that will run in both octave and matlab.

Why?

Consider the following case. You have found some useful code that is currently matlab specific and want to port it into octave. You may need, or want, to make changes, either due to missing functions in octave or to use simpler/faster/etc octave specific functions. If you make these changes while keeping it matlab compatibility you can send the modified code, now both matlab and octave compatible, upstream. This will make contributions between the octave and matlab community easier. It will be easier to apply changes/bug fixes that both you and the upstream developer makes.

Are we running octave?

The most commonly used trick is to create a subfunction that will check whether we are in octave or not, and use it as condition to run selected block of code that are incompatible.

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
end