# Editing Compatibility

Jump to navigation
Jump to search

**Warning:** You are not logged in. Your IP address will be publicly visible if you make any edits. If you **log in** or **create an account**, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision | Your text | ||

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. | |

+ | |||

+ | =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 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 | ||

+ | %% 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</syntaxhighlight>}} | ||

+ | |||

+ | |||

+ | == Calling an internal octave function (even though you shouldn't) == | ||

+ | |||

+ | Octave internal functions start with underscores (ie __delaunayn__ ). Normally, user programs | ||

+ | shouldn't call these, but sometimes, for testing, you may want to do that. The problem is | ||

+ | that matlab can't accept function names with underscores. The trick is to use feval | ||

+ | |||

+ | {{Code|code with underscores|<syntaxhighlight lang="matlab"> | ||

+ | if isoctave () | ||

+ | T = feval('__delaunayn__', opts) | ||

+ | else | ||

+ | % matlab code here | ||

+ | end | ||

+ | % Code to check the results are compatible</syntaxhighlight>}} |