FAQ: Difference between revisions
→Octave extensions
Line 805: | Line 805: | ||
===Octave extensions=== | ===Octave extensions=== | ||
The extensions in Octave over | The extensions in Octave over MATLAB syntax are very useful, but might cause issues when sharing with Matlab users. A list of the major extensions that should be avoided to be compatible with Matlab are: | ||
Comments in | Comments in Octave can be marked with {{Codeline|#}}. This allows POSIX systems to have the first line as {{Codeline|#! octave -q}} and mark the script itself executable. MATLAB doesn't have this feature due to the absence of comments starting with {{Codeline|#}}". | ||
Code blocks like if, for, while, etc can be terminated with block specific terminations like endif. | 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 {{Codeline|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 {{Codeline|rethrow (lasterror ())}} in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. | Octave has a lisp-like {{Codeline|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 {{Codeline|rethrow (lasterror ())}} in Matlab, however rethrow and lasterror are only available in Octave 2.9.10 and later. MATLAB 2008a also introduced {{Codeline|OnCleanUp}} that is similar to {{Codeline|unwind_protect}}, 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 {{Codeline|rethrow (lasterror ())}} 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 {{Codeline|rethrow (lasterror ())}} can not guarantee that global variables will be correctly reset, as it won't catch user interrupts with Ctrl-C. For example | ||
global a | |||
a = 1; | |||
try | |||
_a = a; | |||
a = 2 | |||
while true | |||
end | |||
catch | |||
fprintf ('caught interrupt\n'); | |||
a = _a; | |||
rethrow (lasterror()); | |||
end | |||
compared to | compared to | ||
global a | |||
a = 1; | |||
unwind_protect | |||
_a = a; | |||
a = 2 | |||
while true | |||
end | |||
unwind_protect_cleanup | |||
fprintf ('caught interrupt\n'); | |||
a = _a; | |||
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. | ||
Line 846: | Line 846: | ||
Indexing can be applied to all objects in Octave and not just variables. Therefore {{Codeline|sin(x)(1:10)}} for example is perfectly valid in Octave but not Matlab. To do the same in Matlab you must do {{Codeline|y = sin(x); y = y([1:10]);}} | Indexing can be applied to all objects in Octave and not just variables. Therefore {{Codeline|sin(x)(1:10)}} for example is perfectly valid in Octave but not Matlab. To do the same in Matlab you must do {{Codeline|y = sin(x); y = y([1:10]);}} | ||
Octave has the operators {{Codeline|++}}, {{Codeline|–-}}, {{Codeline|-=}}, {{Codeline|+=}}, {{Codeline|*=}}, etc. As | Octave has the operators {{Codeline|++}}, {{Codeline|–-}}, {{Codeline|-=}}, {{Codeline|+=}}, {{Codeline|*=}}, 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 {{Codeline|\n}} (newline), {{Codeline|\t}} (tab), etc are interpreted in double quoted strings but not single quoted strings. This difference is important on Windows platforms where the {{Codeline|\}} character is used in path names, and so single quoted strings should be used in paths. | 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 {{Codeline|\n}} (newline), {{Codeline|\t}} (tab), etc are interpreted in double quoted strings but not single quoted strings. This difference is important on Windows platforms where the {{Codeline|\}} 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. | ||
=GUI= | =GUI= |