Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 78

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>@sc{Matlab} compatibility - Frequently asked questions about Octave (with answers)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Frequently asked questions about Octave (with answers)">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="prev" href="Using-Octave.html#Using-Octave" title="Using Octave">
<link rel="next" href="Index.html#Index" title="Index">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="%3cspan-class%3d%22sc%22%3eMatlab%3c%2fspan%3e-compatibility"></a>
<a name="g_t_003cspan-class_003d_0022sc_0022_003eMatlab_003c_002fspan_003e-compatibility"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Index.html#Index">Index</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Using-Octave.html#Using-Octave">Using Octave</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
</div>

<h2 class="chapter">11 Porting programs from <span class="sc">Matlab</span> to Octave</h2>

<p><a name="index-g_t_0040sc_007bMatlab_007d-compatibility-36"></a><a name="index-Compatibility-with-_0040sc_007bMatlab_007d-37"></a>
People often ask

   <blockquote>
I wrote some code for <span class="sc">Matlab</span>, and I want to get it running under
Octave.  Is there anything I should watch out for? 
</blockquote>

<p class="noindent">or alternatively

   <blockquote>
I wrote some code in Octave, and want to share it with <span class="sc">Matlab</span>
users.  Is there anything I should watch out for? 
</blockquote>

<p class="noindent">which is not quite the same thing.  There are still a number of
differences between Octave and <span class="sc">Matlab</span>, however in general
differences between the two are considered as bugs.  Octave might
consider that the bug is in <span class="sc">Matlab</span> and do nothing about it, but
generally functionality is almost identical.  If you find a difference
between Octave behavior and <span class="sc">Matlab</span>, then you should send a
description of this difference (with code illustrating the difference,
if possible) to <a href="http://bugs.octave.org">http://bugs.octave.org</a>.

   <p>Furthermore, Octave adds a few syntactical extensions to <span class="sc">Matlab</span>
that might cause some issues when exchanging files between <span class="sc">Matlab</span>
and Octave users. As both Octave and <span class="sc">Matlab</span> are under constant
development the information in this section is subject to change at
anytime.

   <p>You should also look at the page
<a href="http://octave.sourceforge.net/packages.html">http://octave.sourceforge.net/packages.html</a> and
<a href="http://octave.sourceforge.net/doc/">http://octave.sourceforge.net/doc/</a> that has a function reference
that is up to date. You can use this function reference to see the
number of octave function that are available and their <span class="sc">Matlab</span>
compatibility.

   <p>The major differences between Octave 3.4.N and  <span class="sc">Matlab</span> R2010b are:

     <ul>
<li>Nested Functions

     <p>Octave has limited support for nested functions. That is

     <pre class="example">          function y = foo (x)
            y = bar(x)
            function y = bar (x)
              y = ...;
            end
          end
</pre>
     <p>is equivalent to

     <pre class="example">          function y = foo (x)
             y = bar(x)
          end
          function y = bar (x)
             y = ...;
          end
</pre>
     <p>The main difference with <span class="sc">Matlab</span> is a matter of scope. While nested
functions have access to the parent function's scope in <span class="sc">Matlab</span>, no
such thing is available in Octave, due to how Octave essentially
&ldquo;un-nests&rdquo; nested functions.

     <p>The authors of Octave consider the nested function scoping rules of
<span class="sc">Matlab</span> to be more problems than they are worth as they introduce
difficult to find bugs as inadvertently modifying a variable in a
nested function that is also used in the parent is particularly easy.

     <li>Differences in core syntax
There a few core <span class="sc">Matlab</span> syntaxes that are not accepted by Octave,
these being

          <ul>
<li>Some limitations on the use of function handles. The major difference is
related to nested function scoping rules (as above) and their use with
function handles.

          <li>Some limitations of variable argument lists on the LHS of an expression,
though the most common types are accepted.

          <li><span class="sc">Matlab</span> classdef object oriented programming is not yet supported,
though work is underway and when development more on to Octave 3.5 this
will be included in the development tree. 
</ul>

     <li>Differences in core functions
A large number of the <span class="sc">Matlab</span> core functions (ie those that are in
the core and not a toolbox) are implemented, and certainly all of the
commonly used ones. There are a few functions that aren't implemented,
usually to do with specific missing Octave functionality (GUI, DLL,
Java, ActiveX, DDE, web, and serial functions). Some of the core
functions have limitations that aren't in the <span class="sc">Matlab</span> version. For
example the <code>sprandn</code> function can not force a particular condition
number for the matrix like <span class="sc">Matlab</span> can.

     <li>Just-In-Time compiler
<span class="sc">Matlab</span> includes a "Just-In-Time" compiler. This compiler allows the
acceleration of for-loops in <span class="sc">Matlab</span> to almost native performance
with certain restrictions. The JIT must know the return type of all
functions called in the loops and so you can't include user functions in
the loop of JIT optimized loops. Octave doesn't have a JIT and so to
some might seem slower than <span class="sc">Matlab</span>. For this reason you must
vectorize your code as much as possible. The MathWorks themselves have a
good document discussing vectorization at
<!-- It would be nice if we had our own guide for this instead of relying -->
<!-- on Matlab documentation. -->
<a href="http://www.mathworks.com/support/tech-notes/1100/1109.html">http://www.mathworks.com/support/tech-notes/1100/1109.html</a>.

     <li>Compiler
On a related point, there is no Octave compiler, and so you can't
convert your Octave code into a binary for additional speed or
distribution. There have been several aborted attempts at creating an
Octave compiler. Should the JIT compiler above ever be implemented, an
Octave compiler should be more feasible. 
<!-- Should we mention here any of the efforts to create a compiler? There -->
<!-- used to be a dead link here to http://www.stud.tu-ilmenau.de/~rueckn/ -->

     <li>Graphic Handles
Up to Octave 2.9.9 there was no support for graphic handles in Octave
itself. In the 3.2.N versions of Octave and beyond the support for
graphics handles is converging towards full compatibility. The
<code>patch</code> function is currently limited to 2-D patches, due to an
underlying limitation in gnuplot, but the experimental OpenGL backend is
starting to see an implementation of 3-D patches.

     <li>GUI
There are no <span class="sc">Matlab</span> compatible GUI functions. There are a number of
bindings from Octave to Tcl/Tk, VTK and Zenity included in the Octave
<!-- Is it too early to mention here the nascent fltk UI buttons? -->
Forge project (<a href="http://octave.sourceforge.net">http://octave.sourceforge.net</a>) for example that can
be used for a GUI, but these are not <span class="sc">Matlab</span> compatible. Work on a
<span class="sc">Matlab</span> compatible GUI is in an alpha stage in the JHandles package
<!-- Is Jhandles still usable? I thought Michael Goffioul had more or less -->
<!-- already disowned it. -->
(<a href="http://octave.sourceforge.net/jhandles/index.html">http://octave.sourceforge.net/jhandles/index.html</a>). This might be
an issue if you intend to exchange Octave code with <span class="sc">Matlab</span> users.

     <li>Simulink
Octave itself includes no Simulink support. Typically the simulink
models lag research and are less flexible, so shouldn't really be used
in a research environment.  However, some <span class="sc">Matlab</span> users that try to
use Octave complain about this lack.  There is a similar package to
simulink for the Octave and R projects available at
<!-- is this project in any state of usability? -->
<a href="http://www.scicraft.org/">http://www.scicraft.org/</a>

     <li>Mex-Files
Octave includes an API to the <span class="sc">Matlab</span> MEX interface. However, as MEX
is an API to the internals of <span class="sc">Matlab</span> and the internals of Octave
differ from <span class="sc">Matlab</span>, there is necessarily a manipulation of the data
to convert from a MEX interface to the Octave equivalent. This is
notable for all complex matrices, where <span class="sc">Matlab</span> stores complex
arrays as real and imaginary parts, whereas Octave respects the C99/C++
standards of co-locating the real/imag parts in memory. Also due to the
way <span class="sc">Matlab</span> allows access to the arrays passed through a pointer,
the MEX interface might require copies of arrays (even non complex
ones).

     <li>Block comments
Block comments denoted by "%{" and "%}" markers are supported by
Octave with some limitations. The major limitation is that block
comments are not supported within [] or {}.

     <li>Mat-File format
There are some differences in the mat v5 file format accepted by Octave. 
<span class="sc">Matlab</span> recently introduced the "-V7.3" save option which is an HDF5
format which is particularly useful for 64-bit platforms where the
standard <span class="sc">Matlab</span> format can not correctly save variables. Octave
accepts HDF5 files, but is not yet compatible with the "-v7.3" versions
produced by <span class="sc">Matlab</span>.

     <p>Although Octave can load inline function handles saved by <span class="sc">Matlab</span>,
it can not yet save them.

     <p>Finally, Some multi-byte Unicode characters aren't yet treated in
mat-files.

     <li>Profiler
Octave doesn't have a profiler. Though there is a patch for a flat
profiler, that might become a real profiler sometime in the future. See
the thread

     <!-- Did this idea go anywhere? Should it be mentioned? -->
     <p><a href="http://octave.1599824.n4.nabble.com/Octave-profiler-td1641945.html#a1641947">http://octave.1599824.n4.nabble.com/Octave-profiler-td1641945.html#a1641947</a>

     <p>for more details.

     <li>Toolboxes
Octave is a community project and so the toolboxes that exist are
donated by those interested in them through the Octave Forge website
(<a href="http://octave.sourceforge.net">http://octave.sourceforge.net</a>). These might be lacking in certain
functionality relative to the <span class="sc">Matlab</span> toolboxes, and might not
exactly duplicate the <span class="sc">Matlab</span> functionality or interface.

     <li>Short-circuit &amp; and | operators
The <code>&amp;</code> and <code>|</code> operators in <span class="sc">Matlab</span> short-circuit when
included in an if statement and not otherwise.  In Octave only the
<code>&amp;&amp;</code> and <code>||</code> short circuit.  Note that this means that

     <pre class="example">            if (a | b)
              ...
            end
</pre>
     <p>and

     <pre class="example">            t = a | b;
            if t
              ...
            end
</pre>
     <p class="noindent">are different in <span class="sc">Matlab</span>. This is really a <span class="sc">Matlab</span> bug, but
there is too much code out there that relies on this behaviour to change
it. Prefer the || and &amp;&amp; operators in if statements if possible. If you
need to use code written for <span class="sc">Matlab</span> that depends on this buggy
behaviour, you can enable it since Octave 3.4.0 with the following
command:

     <pre class="example">            do_braindead_shortcircuit_evaluation(1)
</pre>
     <p>Note that the difference with <span class="sc">Matlab</span> is also significant when
either argument is a function with side effects or if the first argument
is a scalar and the second argument is an empty matrix. For example,
note the difference between

     <pre class="example">            t = 1 | [];          ## results in [], so...
            if (t) 1, end        ## in if ([]), this is false.
</pre>
     <p>and

     <pre class="example">            if (1 | []) 1, end   ## short circuits so condition is true.
</pre>
     <p>Another case that is documented in the <span class="sc">Matlab</span> manuals is that

     <pre class="example">            t = [1, 1] | [1, 2, 3];          ## error
            if ([1, 1] | [1, 2, 3]) 1, end   ## OK
</pre>
     <p>Also <span class="sc">Matlab</span> requires the operands of &amp;&amp; and || to be scalar values
but Octave does not (it just applies the rule that for an operand to be
considered true, every element of the object must be nonzero or
logically true).

     <p>Finally, note the inconsistence of thinking of the condition of an if
statement as being equivalent to <code>all(X(:))</code> when <var>X</var> is a
matrix.  This is true for all cases EXCEPT empty matrices:

     <pre class="example">            if ([0, 1]) == if (all ([0, 1]))   ==&gt;  i.e., condition is false.
            if ([1, 1]) == if (all ([1, 1]))   ==&gt;  i.e., condition is true.
</pre>
     <p>However,

     <pre class="example">            if ([]) != if (all ([]))
</pre>
     <p>because <code>samp ([]) == 1</code> because, despite the name, it is really
returning true if none of the elements of the matrix are zero, and since
there are no elements, well, none of them are zero. This is an example
of vacuous truth. But, somewhere along the line, someone decided that if
<code>([])</code> should be false. Mathworks probably thought it just looks
wrong to have <code>[]</code> be true in this context even if you can use
logical gymnastics to convince yourself that "all" the elements of a
matrix that doesn't actually have any elements are nonzero. Octave
however duplicates this behavior for if statements containing empty
matrices.

     <li>Solvers for singular, under- and over-determined matrices

     <p><span class="sc">Matlab</span>'s solvers as used by the operators mldivide (\) and mrdivide
(/), use a different approach than Octave's in the case of singular,
under-, or over-determined matrices. In the case of a singular matrix,
<span class="sc">Matlab</span> returns the result given by the LU decomposition, even
though the underlying solver has flagged the result as erroneous. Octave
has made the choice of falling back to a minimum norm solution of
matrices that have been flagged as singular which arguably is a better
result for these cases.

     <p>In the case of under- or over-determined matrices, Octave continues to
use a minimum norm solution, whereas <span class="sc">Matlab</span> uses an approach that
is equivalent to

     <pre class="example">          function x = mldivide (A, b)
            [Q, R, E] = qr(A);
            x = [A \ b, E(:, 1:m) * (R(:, 1:m) \ (Q' * b))]
          end
</pre>
     <p class="noindent">While this approach is certainly faster and uses less memory than
Octave's minimum norm approach, this approach seems to be inferior in
other ways.

     <p>A numerical question arises: how big can the null space component
become, relative to the minimum-norm solution? Can it be nicely bounded,
or can it be arbitrarily big? Consider this example:

     <pre class="example">          m = 10;
          n = 10000;
          A = ones(m, n) + 1e-6 * randn(m,n);
          b = ones(m, 1) + 1e-6 * randn(m,1);
          norm(A \ b)
</pre>
     <p class="noindent">while Octave's minimum-norm values are around 3e-2, <span class="sc">Matlab</span>'s
results are 50-times larger. For another issue, try this code:

     <pre class="example">          m = 5;
          n = 100;
          j = floor(m * rand(1, n)) + 1;
          b = ones(m, 1);
          A = zeros(m, n);
          A(sub2ind(size(A),j,1:n)) = 1;
          x = A \ b;
          [dummy,p] = sort(rand(1,n));
          y = A(:,p)\b;
          norm(x(p)-y)
</pre>
     <p class="noindent">It shows that unlike in Octave, mldivide in <span class="sc">Matlab</span> is not invariant
with respect to column permutations. If there are multiple columns of
the same norm, permuting columns of the matrix gets you different
result than permuting the solution vector. This will surprise many
users.

     <p>Since the mldivide (\) and mrdivide (/) operators are often part of a
more complex expression, where there is no room to react to warnings or
flags, it should prefer intelligence (robustness) to speed, and so the
Octave developers are firmly of the opinion that Octave's approach for
singular, under- and over-determined matrices is a better choice that
<span class="sc">Matlab</span>'s

     <li>Octave extensions
The extensions in Octave over <span class="sc">Matlab</span> syntax are
very useful, but might cause issues when sharing with <span class="sc">Matlab</span> users. 
A list of the major extensions that should be avoided to be compatible
with <span class="sc">Matlab</span> are

          <ul>
<li>Comments in octave can be marked with &lsquo;<samp><span class="samp">#</span></samp>&rsquo;. This allows POSIX
systems to have the first line as &lsquo;<samp><span class="samp">#! octave -q</span></samp>&rsquo; and mark the
script itself executable. <span class="sc">Matlab</span> doesn't have this feature due to
the absence of comments starting with &lsquo;<samp><span class="samp">#</span></samp>&rsquo;".

          <li>Code blocks like <code>if</code>, <code>for</code>, <code>while</code>, etc can be
terminated with block specific terminations like <code>endif</code>. 
<span class="sc">Matlab</span> doesn't have this and all blocks must be terminated with
<code>end</code>.

          <li>Octave has a lisp like <code>unwind_protect</code> block that allows blocks of
code that terminate in an error to ensure that the variables that are
touched are restored. You can do something similar with
<code>try</code>/<code>catch</code> combined with &lsquo;<samp><span class="samp">rethrow (lasterror ())</span></samp>&rsquo; in
<span class="sc">Matlab</span>, however rethrow and lasterror are only available in Octave
2.9.10 and later. <span class="sc">Matlab</span> 2008a also introduced <code>OnCleanUp</code>
that is similar to <code>unwind_protect</code>, except that the object created
by this function has to be explicitly cleared in order for the cleanup
code to run.

          <p>Note that using <code>try</code>/<code>catch</code> combined with &lsquo;<samp><span class="samp">rethrow
(lasterror ())</span></samp>&rsquo; can not guarantee that global variables will be
correctly reset, as it won't catch user interrupts with Ctrl-C. For
example

          <pre class="example">                 global a
                 a = 1;
                 try
                   _a = a;
                   a = 2
                   while true
                   end
                 catch
                   fprintf ('caught interrupt\n');
                   a = _a;
                   rethrow (lasterror());
                 end
</pre>
          <p class="noindent">compared to

          <pre class="example">                 global a
                 a = 1;
                 unwind_protect
                   _a = a;
                   a = 2
                   while true
                   end
                 unwind_protect_cleanup
                   fprintf ('caught interrupt\n');
                   a = _a;
                 end
</pre>
          <p>Typing Ctrl-C in the first case returns the user directly to the
prompt, and the variable "a" is not reset to the saved value. In the
second case the variable "a" is reset correctly.  Therefore <span class="sc">Matlab</span>
gives no safe way of temporarily changing global variables.

          <li>Indexing can be applied to all objects in Octave and not just
variable. Therefore <code>sin(x)(1:10);</code> for example is perfectly valid
in Octave but not <span class="sc">Matlab</span>. To do the same in <span class="sc">Matlab</span> you must do
<code>y = sin(x); y = y([1:10]);</code>

          <li>Octave has the operators "++", "&ndash;", "-=", "+=", "*=", etc.  As
<span class="sc">Matlab</span> doesn't, if you are sharing code these should be avoided.

          <li>Character strings in Octave can be denoted with double or single
quotes. There is a subtle difference between the two in that escaped
characters like <code>\n</code> (newline), <code>\t</code> (tab), etc are
interpreted in double quoted strings but not single quoted strings. This
difference is important on Windows platforms where the "\" character is
used in path names, and so single quoted strings should be used in
paths. <span class="sc">Matlab</span> doesn't have double quoted strings and so they should
be avoided if the code will be transferred to a <span class="sc">Matlab</span> user. 
</ul>

   </ul>

   </body></html>