comparison doc/faq/Octave-FAQ.texi @ 6612:89f3a84a0da1

[project @ 2007-05-09 21:17:45 by jwe]
author jwe
date Wed, 09 May 2007 21:17:45 +0000
parents 2c19eaa2c6f7
children 673686daec87
comparison
equal deleted inserted replaced
6611:9197bb0a2738 6612:89f3a84a0da1
627 @chapter Porting programs from @sc{Matlab} to Octave 627 @chapter Porting programs from @sc{Matlab} to Octave
628 628
629 @cindex @sc{Matlab} compatibility 629 @cindex @sc{Matlab} compatibility
630 @cindex Compatibility with @sc{Matlab} 630 @cindex Compatibility with @sc{Matlab}
631 631
632 People often ask
633
632 ``I wrote some code for @sc{Matlab}, and I want to get it running under 634 ``I wrote some code for @sc{Matlab}, and I want to get it running under
633 Octave. Is there anything I should watch out for?'' 635 Octave. Is there anything I should watch out for?''
634 636
635 The differences between Octave and @sc{Matlab} typically fall into one of 637 or alternatively
636 three categories: 638
637 639 ``I wrote some code in Octave, and want to share it with @sc{Matlab}
638 @enumerate 640 users. Is there anything I should watch out for?''
639 @item 641
640 Irrelevant. 642 which is not quite the same thing. There are still a number of
641 643 differences between Octave and @sc{Matlab}, however in general
642 @item 644 differences between the two are considered as bugs. Octave might
643 Known differences, perhaps configurable with a user preference variable. 645 consider that the bug is in @sc{Matlab} and do nothing about it, but
644 646 generally functionality is almost identical. If you find a difference
645 @item 647 between Octave behavior and @sc{Matlab}, then you should send a
646 Unknown differences.
647 @end enumerate
648
649 The first category, irrelevant differences, do not affect computations
650 and most likely do not affect the execution of function files.
651
652 The differences of the second category are usually because the authors
653 of Octave decided on a better (subjective) implementation than the way
654 @sc{Matlab} works. To make Octave more @sc{Matlab}-compatible, use the
655 command line option @samp{--traditional} (though really, with the latest
656 2.9.N version, most of the things that the @samp{--traditional} option
657 does are just cosmetic.
658
659 The third category of differences is (hopefully) shrinking. If you find
660 a difference between Octave behavior and @sc{Matlab}, then you should send a
661 description of this difference (with code illustrating the difference, 648 description of this difference (with code illustrating the difference,
662 if possible) to @email{bug@@octave.org}. 649 if possible) to @email{bug@@octave.org}.
650
651 Furthermore, Octave adds a few syntactical extensions to Matlab that
652 might cause some issues when exchanging files between Matlab and Octave
653 users. As both Octave and @sc{Matlab} are under constant development the
654 information in this section is subject to change at anytime.
655
656 You should also look at the page
657 @url{http://octave.sourceforge.net/packages.html} and
658 @url{http://octave.sourceforge.net/doc/} that has a function reference
659 that is up to date. You can use this function reference to see the
660 number of octave function that are available and their @sc{Matlab}
661 compatibility.
662
663 The major differences between Octave 2.9.10 and @sc{Matlab} v7.3 are:
664
665 @itemize @bullet
666 @item Nested Functions
667
668 Octave doesn't yet have nested functions. That if
669
670 @example
671 @group
672 function y = foo (x)
673 y = bar(x)
674 function y = bar (x)
675 y = @dots{};
676 end
677 end
678 @end group
679 @end example
680
681 There was discussion in Octave of having these even prior to @sc{Matlab},
682 and the decision was made not to have these in Octave at the time for
683 compatibility. The above written with sub-functions functions would be
684
685 @example
686 @group
687 function y = foo (x)
688 y = bar(x)
689 end
690 function y = bar (x)
691 y = @dots{};
692 end
693 @end group
694 @end example
695
696 Now that @sc{Matlab} has recently introduced nested functions, Octave will
697 probably have them soon as well. Till then nested functions in Octave
698 are treated as sub-functions with the same scoping rules as
699 sub-functions.
700
701 The authors of Octave consider the nested function scoping rules of
702 Matlab to be more problems than they are worth as they introduce
703 diffiult to find bugs as inadvertantly modifying a variable in a
704 nested function that is also used in the parent is particularly easy.
705
706 @item Differences in core syntax
707
708 There a few core @sc{Matlab} syntaxes that are not accepted by Octave,
709 these being
710
711 @itemize @bullet
712 @item
713 Some limitations on the use of function handles. The major difference is
714 related to nested function scoping rules (as above) and there use with
715 function handles.
716
717 @item
718 Some limitations of variable argument lists on the LHS of an expression,
719 though the most common types are accepted.
720
721 @item
722 @sc{Matlab} object-oriented classes with overloading etc through the
723 subasgn and "@@" directories is not treated by Octave. There is work to
724 remove this restriction in the future.
725
726 @item
727 @sc{Matlab} private directories are not treated, though as for classes,
728 this will change in the near future. This restriction alleviated by
729 using addpath on the private directories, but this makes the private
730 directories visible in the global scope.
731 @end itemize
732
733 @item Differences in core functions
734
735 A large number of the @sc{Matlab} core functions (ie those that are in
736 the core and not a toolbox) are implemented, and certainly all of the
737 commonly used ones. There are a few functions that aren't implemented,
738 for example condest or to do with specific missing Octave functionality
739 (gui, dll, java, activex, dde, web, and serial functions). Some of the
740 core functions have limitations that aren't in the @sc{Matlab}
741 version. For example the interpN function can only do linear
742 interpolation and not cubic spline, etc.. However the interp and interp2
743 function can.
744
745 @item Just-In-Time compiler
746
747 @sc{Matlab} includes a "Just-In-Time" compiler. This compiler allows the
748 acceleration of for-loops in @sc{Matlab} to almost native performance with
749 certain restrictions. The JIT must know the return type of all functions
750 called in the loops and so you can't include user functions in the loop
751 of JIT optimized loops. Octave doesn't have a JIT and so to some might
752 seem slower than @sc{Matlab}. For this reason you must vectorize your code as
753 much as possible. @sc{Matlab} themselves produces a good document discussing
754 vectorization at
755 @url{http://www.mathworks.com/support/tech-notes/1100/1109.html}.
756
757 @item Compiler
758
759 On a related point, there is no Octave compiler, and so you can't
760 convert your Octave code into a binary for additional speed or
761 distribution. There is an example of how to do this at
762 @url{http://www.stud.tu-ilmenau.de/~rueckn/}, but this is a very early
763 example code and would need lots of work to complete it.
764
765 @item Graphic Handles
766
767 Up to Octave 2.9.9 there was no support for graphic handles in Octave
768 itself. There is in Octave 2.9.10 and later, and so the graphics between
769 Octave and @sc{Matlab} are currently in the process of converging to a
770 common interface. Note that the basic graphic handle stuff is in place
771 in 2.9.10, but not graphics objects like "patch", "barseries", etc.
772
773 @item GUI
774
775 There is no @sc{Matlab} compatible GUI functions. There are a number of
776 bindings from Octave to tcl/tk, vtk and zenity included in the
777 octave-forge project (@url{http://octave.sourceforge.net}) for example
778 that can be used for a GUI, but these are not @sc{Matlab}
779 compatible. This might be an issue if you intend to exchange Octave code
780 with @sc{Matlab} users.
781
782 @item Simulink
783
784 Octave itself includes no Simulink support. Typically the simulink
785 models lag research and are less flexible, so shouldn't really be used
786 in a research environment. However, some @sc{Matlab} users that try to use
787 Octave complain about this lack. There is a similar package to simulink
788 for the Octave and R projects available at @url{http://www.scicraft.org/}
789
790 @item Mex-Files
791
792 Octave includes an API to the matlab MEX interface. However, as MEX is
793 an API to the internals of @sc{Matlab} and the internals of Octave differ to
794 @sc{Matlab}, there is necessarily a manipulation of the data to convert from
795 a MEX interface to the Octave equivalent. This is notable for all
796 complex matrices, where matlab stores complex arrays as real and
797 imaginary parts, whereas octave respects the C99/C++ standards of
798 co-locating the real/imag parts in memory. Also due to the way matlab
799 allows access to the arrays passed through a pointer, the MEX interface
800 might require that copies of arrays (even non complex ones). There were
801 some serious memory leaks in the MEX API in Octave up to version 2.9.9,
802 and version 2.9.10 or later should be used if posible.
803
804 @item Block comments
805
806 @sc{Matlab} recently included the possibility to have block comments. With
807 the "%@{" and "%@}" markers. Octave doesn't yet have block comments, but
808 might in the future.
809
810 @item Mat-File format
811
812 There are some differences in the mat v5 file format accepted by
813 Octave. @sc{Matlab} recently introduced the "-V7.3" save option which is
814 an hdf5 format which is particularly useful for 64-bit platforms where
815 the standard matlab format can not correctly save variables.. Octave
816 accepts hdf5 files, but is not yet compatible with the "-v7.3" versions
817 produced by @sc{Matlab}.
818
819 Up to version 2.9.10, Octave can't load/save the inline or function
820 handles saved in mat-files by @sc{Matlab} (though can in its own
821 format). There is some work to fix this, but the @sc{Matlab} function handle
822 format in mat-files is undocumented and this is therefore not easy to
823 address.
824
825 Finally, Some multi-byte unicode characters aren't yet treated in
826 mat-files.
827
828 @item Profiler
829
830 Octave doesn't have a profiler. Though there is a patch for a flat
831 profiler, that might become a real profiler sometime in the future. see
832 the thread
833
834 @url{http://www.cae.wisc.edu/pipermail/octave-maintainers/2007-January/001685.html}
835
836 for more details
837
838 @item Toolboxes
839
840 Octave is a community project and so the toolboxes that exist are
841 donated by those interested in them through the octave-forge website
842 (@url{http://octave.sourceforge.net}). These might be lacking in certain
843 functionality relative to the @sc{Matlab} toolboxes, and might not
844 exactly duplicate the matlab functionality or interface.
845
846 @item Short-circuit & and | operators
847
848 The & and | operators in @sc{Matlab} short-circuit when included in an if
849 statemant and not otherwise. Whereas in Octave only the && and ||
850 short circuit. This is due to the absence of && and || in @sc{Matlab}
851 till recently. Note that this means that
852
853 @example
854 @group
855 if (a | b)
856 @dots{}
857 end
858 @end group
859 @end example
860
861 and
862
863 @example
864 @group
865 t = a | b;
866 if t
867 @dots{}
868 end
869 @end group
870 @end example
871
872 are different in @sc{Matlab}. This is really a @sc{Matlab} bug, but
873 there is too much code out there that relies on this behavior to change
874 it. Prefer the || and && operators in if statements if possible.
875
876 Note that the difference is also significant when either argument is a
877 function with side effects or if the first argument is a scalar and the
878 second argument is an empty matrix. For example, note the difference
879 between
880
881 @example
882 @group
883 t = 1 | []; ## results in [], so...
884 if (t) 1, end ## in if ([]), this is false.
885 @end group
886 @end example
887
888 and
889
890 @example
891 if (1 | []) 1, end ## short circuits so condition is true.
892 @end example
893
894 Another case that is documented in the @sc{Matlab} manuals is that
895
896 @example
897 @group
898 t = [1, 1] | [1, 2, 3]; ## error
899 if ([1, 1] | [1, 2, 3]) 1, end ## OK
900 @end group
901 @end example
902
903 Also @sc{Matlab} requires the operands of && and || to be scalar values but
904 Octave does not (it just applies the rule that for an operand to be
905 considered true, every element of the object must be nonzero or
906 logically true).
907
908 Finally, note the inconsistence of thinking of the condition of an if
909 statement as being equivalent to @code{all(X(:))} when @var{X} is a
910 matrix. This is true for all cases EXCEPT empty matrices:
911
912 @example
913 @group
914 if ([0, 1]) == if (all ([0, 1])) ==> i.e., condition is false.
915 if ([1, 1]) == if (all ([1, 1])) ==> i.e., condition is true.
916 @end group
917 @end example
918
919 However,
920
921 @example
922 if ([]) != if (all ([]))
923 @end example
924
925 because @code{all ([]) == 1} (because, despite the name, it is really
926 returning true if none of the elements of the matrix are zero, and since
927 there are no elements, well, none of them are zero). But, somewhere
928 along the line, someone decided that if @code{([])} should be false.
929 Mathworks probably thought it just looks wrong to have @code{[]} be true
930 in this context even if you can use logical gymnastics to convince
931 yourself that "all" the elements of a matrix that doesn't actually have
932 any elements are nonzero. Octave however duplicates this behavior for if
933 statements containing empty matrices.
934
935 @item Octave extensions
936
937 The extensions in Octave over @sc{Matlab} syntax are
938 very useful, but might cause issues when sharing with @sc{Matlab} users.
939 A list of the major extensions that should be avoided to be compatible
940 with @sc{Matlab} are
941
942 @itemize @bullet
943 @item
944 Comments in octave can be marked with "#". This allows POSIX
945 systems to have the first line as "#! octave -q" and mark the script
946 itself executable. @sc{Matlab} doesn't have this feature due the the
947 absence of comments starting with "#"
948
949 @item
950 Code blocks like if, for, while, etc can be terminated with block
951 specific terminations like "endif". @sc{Matlab} doesn't have this and
952 all blocks must be terminated with "end"
953
954 @item
955 Octave has a lisp like unwind_protect block that allows blocks of
956 code that terminate in an error to ensure that the variables that
957 are touched are restored. You can do something similar with
958 try/catch combined with rethrow(lasterror()) in @sc{Matlab}, however
959 rethrow and lasterror were only just introduced in Octave and so
960 aren't in 2.9.9, but are in 2.9.10.
961
962 Note that using try/catch combined with rethrow(lasterror()) can not
963 guarentee that global variables will be correctly reset, as it won't
964 catch user interrupts with Ctrl-C. For example
965
966 @example
967 @group
968 global a
969 a = 1;
970 try
971 _a = a;
972 a = 2
973 while true
974 end
975 catch
976 fprintf ('caught interrupt\n');
977 a = _a;
978 rethrow (lasterror());
979 end
980 @end group
981 @end example
982
983 @noindent
984 compared to
985
986 @example
987 @group
988 global a
989 a = 1;
990 unwind_protect
991 _a = a;
992 a = 2
993 while true
994 end
995 unwind_protect_cleanup
996 fprintf ('caught interrupt\n');
997 a = _a;
998 end
999 @end group
1000 @end example
1001
1002 Typing Ctrl-C in the first case returns the user directly to the
1003 prompt, and the variable "a" is not reset to the save value. In the
1004 second case the variable "a" is reset correctly. Therefore matlab
1005 gives no save way of temporarily changing global variables.
1006
1007 @item
1008 Indexing can be applied to all objects in Octave and not just
1009 variable. Therefore @code{sin(x)(1:10);} for example is perfectly valid
1010 in Octave but not @sc{Matlab}. To do the same in @sc{Matlab} you must do
1011 @code{y = sin(x); y = y([1:10]);}
1012
1013 @item
1014 Octave has the operators "++", "--", "-=", "+=", "*=", etc. As
1015 @sc{Matlab} doesn't, if you are sharing code these should be avoided.
1016
1017 @item
1018 Strings in Octave can be denoted with double or single quotes. There is
1019 a subtle difference between the two in that escaped characters like \n,
1020 etc are interpreted in double quoted strings but not single quoted
1021 strings. This difference is important on Windows platforms where the "\"
1022 character is used in path names, and so single quoted strings should be
1023 used in paths. @sc{Matlab} doesn't have double quoted strings and so
1024 they should be avoided if the code will be transfered to a matlab user.
1025 @end itemize
1026
1027 @end itemize
663 1028
664 @node Index 1029 @node Index
665 @appendix Concept Index 1030 @appendix Concept Index
666 1031
667 @printindex cp 1032 @printindex cp