Mercurial > octave-nkf
comparison doc/interpreter/dynamic.txi @ 7081:503001863427
[project @ 2007-10-31 01:08:14 by jwe]
author | jwe |
---|---|
date | Wed, 31 Oct 2007 01:09:28 +0000 |
parents | fd42779a8428 |
children | 81bed50b9feb |
comparison
equal
deleted
inserted
replaced
7080:7e465260a48f | 7081:503001863427 |
---|---|
351 @subsection Character Strings in Oct-Files | 351 @subsection Character Strings in Oct-Files |
352 | 352 |
353 In Octave a character string is just a special @code{Array} class. | 353 In Octave a character string is just a special @code{Array} class. |
354 Consider the example | 354 Consider the example |
355 | 355 |
356 @examplefile{stringdemo.cc} | 356 @longexamplefile{stringdemo.cc} |
357 | 357 |
358 An example of the of the use of this function is | 358 An example of the of the use of this function is |
359 | 359 |
360 @example | 360 @example |
361 @group | 361 @group |
379 @code{octave_value} contains a single or double quoted string an example is | 379 @code{octave_value} contains a single or double quoted string an example is |
380 | 380 |
381 @example | 381 @example |
382 @group | 382 @group |
383 if (args(0).is_sq_string ()) | 383 if (args(0).is_sq_string ()) |
384 octave_stdout << "First argument is a singularly quoted string\n"; | 384 octave_stdout << |
385 "First argument is a singularly quoted string\n"; | |
385 else if (args(0).is_dq_string ()) | 386 else if (args(0).is_dq_string ()) |
386 octave_stdout << "First argument is a doubly quoted string\n"; | 387 octave_stdout << |
388 "First argument is a doubly quoted string\n"; | |
387 @end group | 389 @end group |
388 @end example | 390 @end example |
389 | 391 |
390 Note however, that both types of strings are represented by the | 392 Note however, that both types of strings are represented by the |
391 @code{charNDArray} type, and so when assigning to an | 393 @code{charNDArray} type, and so when assigning to an |
440 with the pair consisting of a @code{std::string} and an octave | 442 with the pair consisting of a @code{std::string} and an octave |
441 @code{Cell} variable. | 443 @code{Cell} variable. |
442 | 444 |
443 A simple example demonstrating the use of structures within oct-files is | 445 A simple example demonstrating the use of structures within oct-files is |
444 | 446 |
445 @examplefile{structdemo.cc} | 447 @longexamplefile{structdemo.cc} |
446 | 448 |
447 An example of its use is | 449 An example of its use is |
448 | 450 |
449 @example | 451 @example |
450 @group | 452 @group |
729 ii++; | 731 ii++; |
730 @} | 732 @} |
731 @} | 733 @} |
732 sm.cidx(j+1) = ii; | 734 sm.cidx(j+1) = ii; |
733 @} | 735 @} |
734 sm.maybe_compress (); // If don't know a-priori the final no of nz. | 736 sm.maybe_compress (); // If don't know a-priori |
737 // the final no of nz. | |
735 @end group | 738 @end group |
736 @end example | 739 @end example |
737 | 740 |
738 @noindent | 741 @noindent |
739 which is probably the most efficient means of creating the sparse matrix. | 742 which is probably the most efficient means of creating the sparse matrix. |
770 ii++; | 773 ii++; |
771 @} | 774 @} |
772 @} | 775 @} |
773 sm.cidx(j+1) = ii; | 776 sm.cidx(j+1) = ii; |
774 @} | 777 @} |
775 sm.maybe_mutate (); // If don't know a-priori the final no of nz. | 778 sm.maybe_mutate (); // If don't know a-priori |
779 // the final no of nz. | |
776 @end group | 780 @end group |
777 @end example | 781 @end example |
778 | 782 |
779 Note that both increasing and decreasing the number of non-zero elements in | 783 Note that both increasing and decreasing the number of non-zero elements in |
780 a sparse matrix is expensive, as it involves memory reallocation. Also as | 784 a sparse matrix is expensive, as it involves memory reallocation. Also as |
794 @example | 798 @example |
795 @group | 799 @group |
796 octave_value_list retval; | 800 octave_value_list retval; |
797 | 801 |
798 SparseMatrix sm = args(0).sparse_matrix_value (); | 802 SparseMatrix sm = args(0).sparse_matrix_value (); |
799 SparseComplexMatrix scm = args(1).sparse_complex_matrix_value (); | 803 SparseComplexMatrix scm = |
804 args(1).sparse_complex_matrix_value (); | |
800 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value (); | 805 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value (); |
801 @dots{} | 806 @dots{} |
802 retval(2) = sbm; | 807 retval(2) = sbm; |
803 retval(1) = scm; | 808 retval(1) = scm; |
804 retval(0) = sm; | 809 retval(0) = sm; |
818 the first is a string representing the variable name to obtain. The | 823 the first is a string representing the variable name to obtain. The |
819 second argument is a boolean argument specifying what to do in the case | 824 second argument is a boolean argument specifying what to do in the case |
820 that no global variable of the desired name is found. An example of the | 825 that no global variable of the desired name is found. An example of the |
821 use of these two functions is | 826 use of these two functions is |
822 | 827 |
823 @examplefile{globaldemo.cc} | 828 @longexamplefile{globaldemo.cc} |
824 | 829 |
825 An example of its use is | 830 An example of its use is |
826 | 831 |
827 @example | 832 @example |
828 @group | 833 @group |
857 @end enumerate | 862 @end enumerate |
858 | 863 |
859 The example below demonstrates an example that accepts all four means of | 864 The example below demonstrates an example that accepts all four means of |
860 passing a function to an oct-file. | 865 passing a function to an oct-file. |
861 | 866 |
862 @examplefile{funcdemo.cc} | 867 @longexamplefile{funcdemo.cc} |
863 | 868 |
864 The first argument to this demonstration is the user supplied function | 869 The first argument to this demonstration is the user supplied function |
865 and the following arguments are all passed to the user function. | 870 and the following arguments are all passed to the user function. |
866 | 871 |
867 @example | 872 @example |
952 segmentation fault. | 957 segmentation fault. |
953 | 958 |
954 An example of the inclusion of a Fortran function in an oct-file is | 959 An example of the inclusion of a Fortran function in an oct-file is |
955 given in the following example, where the C++ wrapper is | 960 given in the following example, where the C++ wrapper is |
956 | 961 |
957 @examplefile{fortdemo.cc} | 962 @longexamplefile{fortdemo.cc} |
958 | 963 |
959 @noindent | 964 @noindent |
960 and the fortran function is | 965 and the fortran function is |
961 | 966 |
962 @examplefile{fortsub.f} | 967 @longexamplefile{fortsub.f} |
963 | 968 |
964 This example demonstrates most of the features needed to link to an | 969 This example demonstrates most of the features needed to link to an |
965 external Fortran function, including passing arrays and strings, as well | 970 external Fortran function, including passing arrays and strings, as well |
966 as exception handling. An example of the behavior of this function is | 971 as exception handling. An example of the behavior of this function is |
967 | 972 |
1014 purposes of parameter checking. These include the methods of the | 1019 purposes of parameter checking. These include the methods of the |
1015 octave_value class like @code{is_real_matrix}, etc, but equally include | 1020 octave_value class like @code{is_real_matrix}, etc, but equally include |
1016 more specialized functions. Some of the more common ones are | 1021 more specialized functions. Some of the more common ones are |
1017 demonstrated in the following example | 1022 demonstrated in the following example |
1018 | 1023 |
1019 @examplefile{paramdemo.cc} | 1024 @longexamplefile{paramdemo.cc} |
1020 | 1025 |
1021 @noindent | 1026 @noindent |
1022 and an example of its use is | 1027 and an example of its use is |
1023 | 1028 |
1024 @example | 1029 @example |
1083 can also be used in oct-files. In conjunction with the exception | 1088 can also be used in oct-files. In conjunction with the exception |
1084 handling of Octave, it is important to enforce that certain code is run | 1089 handling of Octave, it is important to enforce that certain code is run |
1085 to allow variables, etc to be restored even if an exception occurs. An | 1090 to allow variables, etc to be restored even if an exception occurs. An |
1086 example of the use of this mechanism is | 1091 example of the use of this mechanism is |
1087 | 1092 |
1088 @examplefile{unwinddemo.cc} | 1093 @longexamplefile{unwinddemo.cc} |
1089 | 1094 |
1090 As can be seen in the example | 1095 As can be seen in the example |
1091 | 1096 |
1092 @example | 1097 @example |
1093 @group | 1098 @group |
1119 @example | 1124 @example |
1120 @group | 1125 @group |
1121 DEFUN_DLD (do_what_i_want, args, nargout, | 1126 DEFUN_DLD (do_what_i_want, args, nargout, |
1122 "-*- texinfo -*-\n\ | 1127 "-*- texinfo -*-\n\ |
1123 @@deftypefn @{Function File@} @{@} do_what_i_say (@@var@{n@})\n\ | 1128 @@deftypefn @{Function File@} @{@} do_what_i_say (@@var@{n@})\n\ |
1124 A function that does what the user actually wants rather than what\n\ | 1129 A function that does what the user actually wants rather\n\ |
1125 they requested.\n\ | 1130 than what they requested.\n\ |
1126 @@end deftypefn") | 1131 @@end deftypefn") |
1127 @{ | 1132 @{ |
1128 @dots{} | 1133 @dots{} |
1129 @} | 1134 @} |
1130 @end group | 1135 @end group |
1343 | 1348 |
1344 An example that demonstration how to work with arbitrary real or complex | 1349 An example that demonstration how to work with arbitrary real or complex |
1345 double precision arrays is given by the file @file{mypow2.c} as given | 1350 double precision arrays is given by the file @file{mypow2.c} as given |
1346 below. | 1351 below. |
1347 | 1352 |
1348 @examplefile{mypow2.c} | 1353 @longexamplefile{mypow2.c} |
1349 | 1354 |
1350 @noindent | 1355 @noindent |
1351 with an example of its use | 1356 with an example of its use |
1352 | 1357 |
1353 @example | 1358 @example |
1357 @result{} 1 | 1362 @result{} 1 |
1358 @end group | 1363 @end group |
1359 @end example | 1364 @end example |
1360 | 1365 |
1361 | 1366 |
1362 The example above uses the @code{mxGetNumberOfElements}, | 1367 The example above uses @code{mxGetDimensions}, |
1363 @code{mxGetNumberOfDimensions} and @code{mxGetDimensions}, to work with | 1368 @code{mxGetNumberOfElements}, @code{mxGetNumberOfDimensions}, to work with |
1364 the dimensional parameters of multi-dimensional arrays. The also exists | 1369 the dimensional parameters of multi-dimensional arrays. The also exists |
1365 the functions @code{mxGetM}, and @code{mxGetN} that probe the number of | 1370 the functions @code{mxGetM}, and @code{mxGetN} that probe the number of |
1366 rows and columns in a matrix. | 1371 rows and columns in a matrix. |
1367 | 1372 |
1368 @node Character Strings in Mex-Files | 1373 @node Character Strings in Mex-Files |
1372 quoted strings within Octave, there is perhaps less complexity in the | 1377 quoted strings within Octave, there is perhaps less complexity in the |
1373 use of strings and character matrices in mex-files. An example of their | 1378 use of strings and character matrices in mex-files. An example of their |
1374 use, that parallels the demo in @file{stringdemo.cc}, is given in the | 1379 use, that parallels the demo in @file{stringdemo.cc}, is given in the |
1375 file @file{mystring.c}, as seen below. | 1380 file @file{mystring.c}, as seen below. |
1376 | 1381 |
1377 @examplefile{mystring.c} | 1382 @longexamplefile{mystring.c} |
1378 | 1383 |
1379 @noindent | 1384 @noindent |
1380 An example of its expected output is | 1385 An example of its expected output is |
1381 | 1386 |
1382 @example | 1387 @example |
1446 @code{mxCreateStructMatrix}, which creates a structure array with a two | 1451 @code{mxCreateStructMatrix}, which creates a structure array with a two |
1447 dimensional matrix, or @code{mxCreateStructArray}. | 1452 dimensional matrix, or @code{mxCreateStructArray}. |
1448 | 1453 |
1449 @example | 1454 @example |
1450 @group | 1455 @group |
1451 mxArray *mxCreateStructArray (int ndims, int *dims, int num_keys, | 1456 mxArray *mxCreateStructArray (int ndims, int *dims, |
1457 int num_keys, | |
1452 const char **keys); | 1458 const char **keys); |
1453 mxArray *mxCreateStructMatrix (int rows, int cols, int num_keys, | 1459 mxArray *mxCreateStructMatrix (int rows, int cols, |
1460 int num_keys, | |
1454 const char **keys); | 1461 const char **keys); |
1455 @end group | 1462 @end group |
1456 @end example | 1463 @end example |
1457 | 1464 |
1458 Accessing the fields of the structure can then be performed with the | 1465 Accessing the fields of the structure can then be performed with the |
1459 @code{mxGetField} and @code{mxSetField} or alternatively with the | 1466 @code{mxGetField} and @code{mxSetField} or alternatively with the |
1460 @code{mxGetFieldByNumber} and @code{mxSetFieldByNumber} functions. | 1467 @code{mxGetFieldByNumber} and @code{mxSetFieldByNumber} functions. |
1461 | 1468 |
1462 @example | 1469 @example |
1463 @group | 1470 @group |
1464 mxArray *mxGetField (const mxArray *ptr, mwIndex index, const char *key); | 1471 mxArray *mxGetField (const mxArray *ptr, mwIndex index, |
1472 const char *key); | |
1465 mxArray *mxGetFieldByNumber (const mxArray *ptr, | 1473 mxArray *mxGetFieldByNumber (const mxArray *ptr, |
1466 mwIndex index, int key_num); | 1474 mwIndex index, int key_num); |
1467 void mxSetField (mxArray *ptr, mwIndex index, | 1475 void mxSetField (mxArray *ptr, mwIndex index, |
1468 const char *key, mxArray *val); | 1476 const char *key, mxArray *val); |
1469 void mxSetFieldByNumber (mxArray *ptr, mwIndex index, | 1477 void mxSetFieldByNumber (mxArray *ptr, mwIndex index, |
1478 includes a Cell Array per field of the structure. | 1486 includes a Cell Array per field of the structure. |
1479 | 1487 |
1480 An example that demonstrates the use of structures in mex-file can be | 1488 An example that demonstrates the use of structures in mex-file can be |
1481 found in the file @file{mystruct.c}, as seen below | 1489 found in the file @file{mystruct.c}, as seen below |
1482 | 1490 |
1483 @examplefile{mystruct.c} | 1491 @longexamplefile{mystruct.c} |
1484 | 1492 |
1485 An example of the behavior of this function within Octave is then | 1493 An example of the behavior of this function within Octave is then |
1486 | 1494 |
1487 @example | 1495 @example |
1488 @group | 1496 @group |
1489 a(1).f1 = "f11"; a(1).f2 = "f12"; a(2).f1 = "f21"; a(2).f2 = "f22"; | 1497 a(1).f1 = "f11"; a(1).f2 = "f12"; |
1498 a(2).f1 = "f21"; a(2).f2 = "f22"; | |
1490 b = mystruct(a) | 1499 b = mystruct(a) |
1491 @result{} field f1(0) = f11 | 1500 @result{} field f1(0) = f11 |
1492 field f1(1) = f21 | 1501 field f1(1) = f21 |
1493 field f2(0) = f12 | 1502 field f2(0) = f12 |
1494 field f2(1) = f22 | 1503 field f2(1) = f22 |
1577 | 1586 |
1578 It is also possible call other Octave functions from within a mex-file | 1587 It is also possible call other Octave functions from within a mex-file |
1579 using @code{mexCallMATLAB}. An example of the use of | 1588 using @code{mexCallMATLAB}. An example of the use of |
1580 @code{mexCallMATLAB} can be see in the example below | 1589 @code{mexCallMATLAB} can be see in the example below |
1581 | 1590 |
1582 @examplefile{myfeval.c} | 1591 @longexamplefile{myfeval.c} |
1583 | 1592 |
1584 If this code is in the file @file{myfeval.c}, and is compiled to | 1593 If this code is in the file @file{myfeval.c}, and is compiled to |
1585 @file{myfeval.mex}, then an example of its use is | 1594 @file{myfeval.mex}, then an example of its use is |
1586 | 1595 |
1587 @example | 1596 @example |