FAQ: Difference between revisions

111 bytes added ,  16 November 2011
Line 638: Line 638:
Code blocks like if, for, while, etc can be terminated with block specific terminations like endif. Matlab doesn't have this and all blocks must be terminated with end.
Code blocks like if, for, while, etc can be terminated with block specific terminations like endif. Matlab doesn't have this and all blocks must be terminated with end.


Octave has a lisp like unwind_protect 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 try/catch combined with <tt>rethrow (lasterror ())</tt> in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. Matlab 2008a also introduced OnCleanUp that is similar to unwind_protect, except that the object created by this function has to be explicitly cleared in order for the cleanup code to run.
Octave has a lisp-like <tt>unwind_protect</tt> 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 try/catch combined with <tt>rethrow (lasterror ())</tt> in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. Matlab 2008a also introduced <tt>OnCleanUp</tt> that is similar to <tt>unwind_protect</tt>, except that the object created by this function has to be explicitly cleared in order for the cleanup code to run.


Note that using try/catch combined with <tt>rethrow (lasterror ())</tt> can not guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example
Note that using try/catch combined with <tt>rethrow (lasterror ())</tt> can not guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example
Line 669: Line 669:
                 end
                 end


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 Matlab gives no safe way of temporarily changing global variables.
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 Matlab gives no safe way of temporarily changing global variables.


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


Octave has the operators "++", "", "-=", "+=", "*=", etc. As Matlab doesn't, if you are sharing code these should be avoided.
Octave has the operators <tt>++</tt>, <tt></tt>, <tt>-=</tt>, <tt>+=</tt>, <tt>*=</tt>, etc. As Matlab doesn't, if you are sharing code these should be avoided.
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 \n (newline), \t (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. Matlab doesn't have double quoted strings and so they should be avoided if the code will be transferred to a Matlab user.
 
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 <tt>\n</tt> (newline), <tt>\t</tt> (tab), etc are interpreted in double quoted strings but not single quoted strings. This difference is important on Windows platforms where the <tt>\</tt> character is used in path names, and so single quoted strings should be used in paths. Matlab doesn't have double quoted strings and so they should be avoided if the code will be transferred to a Matlab user.