home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / doc / octave.i05 (.txt) < prev    next >
GNU Info File  |  2000-01-15  |  51KB  |  974 lines

  1. This is Info file octave, produced by Makeinfo-1.64 from the input file
  2. octave.tex.
  3. START-INFO-DIR-ENTRY
  4. * Octave: (octave).    Interactive language for numerical computations.
  5. END-INFO-DIR-ENTRY
  6.    Copyright (C) 1996, 1997 John W. Eaton.
  7.    Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided that
  12. the entire resulting derived work is distributed under the terms of a
  13. permission notice identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions.
  17. File: octave,  Node: Script Files,  Next: Dynamically Linked Functions,  Prev: Function Files,  Up: Functions and Scripts
  18. Script Files
  19. ============
  20.    A script file is a file containing (almost) any sequence of Octave
  21. commands.  It is read and evaluated just as if you had typed each
  22. command at the Octave prompt, and provides a convenient way to perform a
  23. sequence of commands that do not logically belong inside a function.
  24.    Unlike a function file, a script file must *not* begin with the
  25. keyword `function'.  If it does, Octave will assume that it is a
  26. function file, and that it defines a single function that should be
  27. evaluated as soon as it is defined.
  28.    A script file also differs from a function file in that the variables
  29. named in a script file are not local variables, but are in the same
  30. scope as the other variables that are visible on the command line.
  31.    Even though a script file may not begin with the `function' keyword,
  32. it is possible to define more than one function in a single script file
  33. and load (but not execute) all of them at once.  To do this, the first
  34. token in the file (ignoring comments and other white space) must be
  35. something other than `function'.  If you have no other statements to
  36. evaluate, you can use a statement that has no effect, like this:
  37.      # Prevent Octave from thinking that this
  38.      # is a function file:
  39.      
  40.      1;
  41.      
  42.      # Define function one:
  43.      
  44.      function one ()
  45.        ...
  46.    To have Octave read and compile these functions into an internal
  47. form, you need to make sure that the file is in Octave's `LOADPATH',
  48. then simply type the base name of the file that contains the commands.
  49. (Octave uses the same rules to search for script files as it does to
  50. search for function files.)
  51.    If the first token in a file (ignoring comments) is `function',
  52. Octave will compile the function and try to execute it, printing a
  53. message warning about any non-whitespace characters that appear after
  54. the function definition.
  55.    Note that Octave does not try to look up the definition of any
  56. identifier until it needs to evaluate it.  This means that Octave will
  57. compile the following statements if they appear in a script file, or
  58. are typed at the command line,
  59.      # not a function file:
  60.      1;
  61.      function foo ()
  62.        do_something ();
  63.      endfunction
  64.      function do_something ()
  65.        do_something_else ();
  66.      endfunction
  67. even though the function `do_something' is not defined before it is
  68. referenced in the function `foo'.  This is not an error because Octave
  69. does not need to resolve all symbols that are referenced by a function
  70. until the function is actually evaluated.
  71.    Since Octave doesn't look for definitions until they are needed, the
  72. following code will always print `bar = 3' whether it is typed directly
  73. on the command line, read from a script file, or is part of a function
  74. body, even if there is a function or script file called `bar.m' in
  75. Octave's `LOADPATH'.
  76.      eval ("bar = 3");
  77.      bar
  78.    Code like this appearing within a function body could fool Octave if
  79. definitions were resolved as the function was being compiled.  It would
  80. be virtually impossible to make Octave clever enough to evaluate this
  81. code in a consistent fashion.  The parser would have to be able to
  82. perform the call to `eval' at compile time, and that would be
  83. impossible unless all the references in the string to be evaluated could
  84. also be resolved, and requiring that would be too restrictive (the
  85. string might come from user input, or depend on things that are not
  86. known until the function is evaluated).
  87.    Although Octave normally executes commands from script files that
  88. have the name `FILE.m', you can use the function `source' to execute
  89. commands from any file.
  90.  - Built-in Function:  source (FILE)
  91.      Parse and execute the contents of FILE.  This is equivalent to
  92.      executing commands from a script file, but without requiring the
  93.      file to be named `FILE.m'.
  94. File: octave,  Node: Dynamically Linked Functions,  Next: Organization of Functions,  Prev: Script Files,  Up: Functions and Scripts
  95. Dynamically Linked Functions
  96. ============================
  97.    On some systems, Octave can dynamically load and execute functions
  98. written in C++.  Octave can only directly call functions written in C++,
  99. but you can also load functions written in other languages by calling
  100. them from a simple wrapper function written in C++.
  101.    Here is an example of how to write a C++ function that Octave can
  102. load, with commentary.  The source for this function is included in the
  103. source distributions of Octave, in the file `examples/oregonator.cc'.
  104. It defines the same set of differential equations that are used in the
  105. example problem of *Note Ordinary Differential Equations::.  By running
  106. that example and this one, we can compare the execution times to see
  107. what sort of increase in speed you can expect by using dynamically
  108. linked functions.
  109.    The function defined in `oregonator.cc' contains just 8 statements,
  110. and is not much different than the code defined in the corresponding
  111. M-file (also distributed with Octave in the file
  112. `examples/oregonator.m').
  113.    Here is the complete text of `oregonator.cc':
  114.    just
  115.      #include <octave/oct.h>
  116.      
  117.      DEFUN_DLD (oregonator, args, ,
  118.        "The `oregonator'.")
  119.      {
  120.        ColumnVector dx (3);
  121.      
  122.        ColumnVector x = args(0).vector_value ();
  123.      
  124.        dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0)
  125.                         - 8.375e-06*pow (x(0), 2));
  126.      
  127.        dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27;
  128.      
  129.        dx(2) = 0.161*(x(0) - x(2));
  130.      
  131.        return octave_value (dx);
  132.      }
  133.    The first line of the file,
  134.      #include <octave/oct.h>
  135. includes declarations for all of Octave's internal functions that you
  136. will need.  If you need other functions from the standard C++ or C
  137. libraries, you can include the necessary headers here.
  138.    The next two lines
  139.      DEFUN_DLD (oregonator, args, ,
  140.        "The `oregonator'.")
  141. declares the function.  The macro `DEFUN_DLD' and the macros that it
  142. depends on are defined in the files `defun-dld.h', `defun.h', and
  143. `defun-int.h' (these files are included in the header file
  144. `octave/oct.h').
  145.    Note that the third parameter to `DEFUN_DLD' (`nargout') is not
  146. used, so it is omitted from the list of arguments to in order to avoid
  147. the warning from gcc about an unused function parameter.
  148. simply declares an object to store the right hand sides of the
  149. differential equation, and
  150.    The statement
  151.      ColumnVector x = args(0).vector_value ();
  152. extracts a column vector from the input arguments.  The variable `args'
  153. is passed to functions defined with `DEFUN_DLD' as an
  154. `octave_value_list' object, which includes methods for getting the
  155. length of the list and extracting individual elements.
  156.    In this example, we don't check for errors, but that is not
  157. difficult.  All of the Octave's built-in functions do some form of
  158. checking on their arguments, so you can check the source code for those
  159. functions for examples of various strategies for verifying that the
  160. correct number and types of arguments have been supplied.
  161.    The next statements
  162.      ColumnVector dx (3);
  163.      
  164.      dx(0) = 77.27 * (x(1) - x(0)*x(1) + x(0)
  165.                       - 8.375e-06*pow (x(0), 2));
  166.      
  167.      dx(1) = (x(2) - x(0)*x(1) - x(1)) / 77.27;
  168.      
  169.      dx(2) = 0.161*(x(0) - x(2));
  170. define the right hand side of the differential equation.  Finally, we
  171. can return `dx':
  172.      return octave_value (dx);
  173. The actual return type is `octave_value_list', but it is only necessary
  174. to convert the return type to an `octave_value' because there is a
  175. default constructor that can automatically create an object of that
  176. type from an `octave_value' object, so we can just use that instead.
  177.    To use this file, your version of Octave must support dynamic
  178. linking.  To find out if it does, type the command `octave_config_info
  179. ("dld")' at the Octave prompt.  Support for dynamic linking is included
  180. if this command returns 1.
  181.    To compile the example file, type the command `mkoctfile
  182. oregonator.cc' at the shell prompt.  The script `mkoctfile' should have
  183. been installed along with Octave.  Running it will create a file called
  184. `oregonator.oct' that can be loaded by Octave.  To test the
  185. `oregonator.oct' file, start Octave and type the command
  186.      oregonator ([1, 2, 3], 0)
  187. at the Octave prompt.  Octave should respond by printing
  188.      ans =
  189.      
  190.         77.269353
  191.         -0.012942
  192.         -0.322000
  193.    You can now use the `oregonator.oct' file just as you would the
  194. `oregonator.m' file to solve the set of differential equations.
  195.    On a 133 MHz Pentium running Linux, Octave can solve the problem
  196. shown in *Note Ordinary Differential Equations:: in about 1.4 second
  197. using the dynamically linked function, compared to about 19 seconds
  198. using the M-file.  Similar decreases in execution time can be expected
  199. for other functions, particularly those that rely on functions like
  200. `lsode' that require user-supplied functions.
  201.    Just as for M-files, Octave will automatically reload dynamically
  202. linked functions when the files that define them are more recent than
  203. the last time that the function was loaded.  Two variables are
  204. available to control how Octave behaves when dynamically linked
  205. functions are cleared or reloaded.
  206.  - Built-in Variable: auto_unload_dot_oct_files
  207.      If the value of `auto_unload_dot_oct_files' is nonzero, Octave will
  208.      automatically unload any `.oct' files when there are no longer any
  209.      functions in the symbol table that reference them.
  210.  - Built-in Variable: warn_reload_forces_clear
  211.      If several functions have been loaded from the same file, Octave
  212.      must clear all the functions before any one of them can be
  213.      reloaded.  If `warn_reload_forces_clear', Octave will warn you
  214.      when this happens, and print a list of the additional functions
  215.      that it is forced to clear.
  216.    Additional examples for writing dynamically linked functions are
  217. available in the files in the `src' directory of the Octave
  218. distribution.  Currently, this includes the files
  219.      balance.cc   fft2.cc      inv.cc       qzval.cc
  220.      chol.cc      filter.cc    log.cc       schur.cc
  221.      colloc.cc    find.cc      lsode.cc     sort.cc
  222.      dassl.cc     fsolve.cc    lu.cc        svd.cc
  223.      det.cc       givens.cc    minmax.cc    syl.cc
  224.      eig.cc       hess.cc      pinv.cc
  225.      expm.cc      ifft.cc      qr.cc
  226.      fft.cc       ifft2.cc     quad.cc
  227. These files use the macro `DEFUN_DLD_BUILTIN' instead of `DEFUN_DLD'.
  228. The difference between these two macros is just that
  229. `DEFUN_DLD_BUILTIN' can define a built-in function that is not
  230. dynamically loaded if the operating system does not support dynamic
  231. linking.  To define your own dynamically linked functions you should use
  232. `DEFUN_DLD'.
  233.    There is currently no detailed description of all the functions that
  234. you can call in a built-in function.  For the time being, you will have
  235. to read the source code for Octave.
  236. File: octave,  Node: Organization of Functions,  Prev: Dynamically Linked Functions,  Up: Functions and Scripts
  237. Organization of Functions Distributed with Octave
  238. =================================================
  239.    Many of Octave's standard functions are distributed as function
  240. files.  They are loosely organized by topic, in subdirectories of
  241. `OCTAVE-HOME/lib/octave/VERSION/m', to make it easier to find them.
  242.    The following is a list of all the function file subdirectories, and
  243. the types of functions you will find there.
  244. `audio'
  245.      Functions for playing and recording sounds.
  246. `control'
  247.      Functions for design and simulation of automatic control systems.
  248. `elfun'
  249.      Elementary functions.
  250. `general'
  251.      Miscellaneous matrix manipulations, like `flipud', `rot90', and
  252.      `triu', as well as other basic functions, like `is_matrix',
  253.      `nargchk', etc.
  254. `image'
  255.      Image processing tools.  These functions require the X Window
  256.      System.
  257.      Input-ouput functions.
  258. `linear-algebra'
  259.      Functions for linear algebra.
  260. `miscellaneous'
  261.      Functions that don't really belong anywhere else.
  262. `plot'
  263.      A set of functions that implement the MATLAB-like plotting
  264.      functions.
  265. `polynomial'
  266.      Functions for manipulating polynomials.
  267. `set'
  268.      Functions for creating and manipulating sets of unique values.
  269. `signal'
  270.      Functions for signal processing applications.
  271. `specfun'
  272.      Special functions.
  273. `special-matrix'
  274.      Functions that create special matrix forms.
  275. `startup'
  276.      Octave's system-wide startup file.
  277. `statistics'
  278.      Statistical functions.
  279. `strings'
  280.      Miscellaneous string-handling functions.
  281. `time'
  282.      Functions related to time keeping.
  283.    % DO NOT EDIT!  Generated automatically by munge-texi.
  284. File: octave,  Node: Error Handling,  Next: Input and Output,  Prev: Functions and Scripts,  Up: Top
  285. Error Handling
  286. **************
  287.    Octave includes several functions for printing error and warning
  288. messages.  When you write functions that need to take special action
  289. when they encounter abnormal conditions, you should print the error
  290. messages using the functions described in this chapter.
  291.  - Built-in Function:  error (TEMPLATE, ...)
  292.      The `error' function formats the optional arguments under the
  293.      control of the template string TEMPLATE using the same rules as
  294.      the `printf' family of functions (*note Formatted Output::.).  The
  295.      resulting message is prefixed by the string `error: ' and printed
  296.      on the `stderr' stream.
  297.      Calling `error' also sets Octave's internal error state such that
  298.      control will return to the top level without evaluating any more
  299.      commands.  This is useful for aborting from functions or scripts.
  300.      If the error message does not end with a new line character,
  301.      Octave will print a traceback of all the function calls leading to
  302.      the error.  For example, given the following function definitions:
  303.           function f () g () end
  304.           function g () h () end
  305.           function h () nargin == 1 || error ("nargin != 1"); end
  306.      calling the function `f' will result in a list of messages that
  307.      can help you to quickly locate the exact location of the error:
  308.           f ()
  309.           error: nargin != 1
  310.           error: evaluating index expression near line 1, column 30
  311.           error: evaluating binary operator `||' near line 1, column 27
  312.           error: called from `h'
  313.           error: called from `g'
  314.           error: called from `f'
  315.      If the error message ends in a new line character, Octave will
  316.      print the message but will not display any traceback messages as
  317.      it returns control to the top level.  For example, modifying the
  318.      error message in the previous example to end in a new line causes
  319.      Octave to only print a single message:
  320.           function h () nargin == 1 || error ("nargin != 1\n"); end
  321.           f ()
  322.           error: nargin != 1
  323.  - Built-in Variable: error_text
  324.      This variable contains the text of error messages that would have
  325.      been printed in the body of the most recent `unwind_protect' or
  326.      `try' statement or the TRY part of the most recent call to the
  327.      `eval' function.  Outside of the `unwind_protect' and `try'
  328.      statements or the `eval' function, or if no error has occurred
  329.      within them, the value of `error_text' is guaranteed to be the
  330.      empty string.
  331.      Note that the message does not include the first `error: ' prefix,
  332.      so that it may easily be passed to the `error' function without
  333.      additional processing(1).
  334.      *Note The try Statement:: and *Note The unwind_protect Statement::.
  335.  - Built-in Variable: beep_on_error
  336.      If the value of `beep_on_error' is nonzero, Octave will try to
  337.      ring your terminal's bell before printing an error message.  The
  338.      default value is 0.
  339.  - Built-in Function:  warning (MSG)
  340.      Print a warning message MSG prefixed by the string `warning: '.
  341.      After printing the warning message, Octave will continue to execute
  342.      commands.  You should use this function should when you want to
  343.      notify the user of an unusual condition, but only when it makes
  344.      sense for your program to go on.
  345.  - Built-in Function:  usage (MSG)
  346.      Print the message MSG, prefixed by the string `usage: ', and set
  347.      Octave's internal error state such that control will return to the
  348.      top level without evaluating any more commands.  This is useful for
  349.      aborting from functions.
  350.      After `usage' is evaluated, Octave will print a traceback of all
  351.      the function calls leading to the usage message.
  352.      You should use this function for reporting problems errors that
  353.      result from an improper call to a function, such as calling a
  354.      function with an incorrect number of arguments, or with arguments
  355.      of the wrong type.  For example, most functions distributed with
  356.      Octave begin with code like this
  357.           if (nargin != 2)
  358.             usage ("foo (a, b)");
  359.           endif
  360.      to check for the proper number of arguments.
  361.    The following pair of functions are of limited usefulness, and may be
  362. removed from future versions of Octave.
  363.  - Function File:  perror (NAME, NUM)
  364.      Print the error message for function NAME corresponding to the
  365.      error number NUM.  This function is intended to be used to print
  366.      useful error messages for those functions that return numeric error
  367.      codes.
  368.  - Function File:  strerror (NAME, NUM)
  369.      Return the text of an error message for function NAME
  370.      corresponding to the error number NUM.  This function is intended
  371.      to be used to print useful error messages for those functions that
  372.      return numeric error codes.
  373.    % DO NOT EDIT!  Generated automatically by munge-texi.
  374.    ---------- Footnotes ----------
  375.    (1)  Yes, it's a kluge, but it seems to be a reasonably useful one.
  376. File: octave,  Node: Input and Output,  Next: Plotting,  Prev: Error Handling,  Up: Top
  377. Input and Output
  378. ****************
  379.    There are two distinct classes of input and output functions.  The
  380. first set are modeled after the functions available in MATLAB.  The
  381. second set are modeled after the standard I/O library used by the C
  382. programming language and offer more flexibility and control over the
  383. output.
  384.    When running interactively, Octave normally sends any output intended
  385. for your terminal that is more than one screen long to a paging program,
  386. such as `less' or `more'.  This avoids the problem of having a large
  387. volume of output stream by before you can read it.  With `less' (and
  388. some versions of `more') you can also scan forward and backward, and
  389. search for specific items.
  390.    Normally, no output is displayed by the pager until just before
  391. Octave is ready to print the top level prompt, or read from the
  392. standard input (for example, by using the `fscanf' or `scanf'
  393. functions).  This means that there may be some delay before any output
  394. appears on your screen if you have asked Octave to perform a
  395. significant amount of work with a single command statement.  The
  396. function `fflush' may be used to force output to be sent to the pager
  397. (or any other stream) immediately.
  398.    You can select the program to run as the pager by setting the
  399. variable `PAGER', and you can turn paging off by setting the value of
  400. the variable `page_screen_output' to 0.
  401.  - Command: more
  402.  - Command: more ON
  403.  - Command: more OFF
  404.      Turn output pagination on or off.  Without an argument, `more'
  405.      toggles the current state.
  406.  - Built-in Variable: PAGER
  407.      The default value is normally `"less"', `"more"', or `"pg"',
  408.      depending on what programs are installed on your system.  *Note
  409.      Installation::.
  410.      When running interactively, Octave sends any output intended for
  411.      your terminal that is more than one screen long to the program
  412.      named by the value of the variable `PAGER'.
  413.  - Built-in Variable: page_screen_output
  414.      If the value of `page_screen_output' is nonzero, all output
  415.      intended for the screen that is longer than one page is sent
  416.      through a pager.  This allows you to view one screenful at a time.
  417.      Some pagers (such as `less'--see *Note Installation::) are also
  418.      capable of moving backward on the output.  The default value is 1.
  419.  - Built-in Variable: page_output_immediately
  420.      If the value of `page_output_immediately' is nonzero, Octave sends
  421.      output to the pager as soon as it is available.  Otherwise, Octave
  422.      buffers its output and waits until just before the prompt is
  423.      printed to flush it to the pager.  The default value is 0.
  424.  - Built-in Function:  fflush (FID)
  425.      Flush output to FID.  This is useful for ensuring that all pending
  426.      output makes it to the screen before some other event occurs.  For
  427.      example, it is always a good idea to flush the standard output
  428.      stream before calling `input'.
  429. * Menu:
  430. * Basic Input and Output::
  431. * C-Style I/O Functions::
  432. File: octave,  Node: Basic Input and Output,  Next: C-Style I/O Functions,  Prev: Input and Output,  Up: Input and Output
  433. Basic Input and Output
  434. ======================
  435. * Menu:
  436. * Terminal Output::
  437. * Terminal Input::
  438. * Simple File I/O::
  439. File: octave,  Node: Terminal Output,  Next: Terminal Input,  Prev: Basic Input and Output,  Up: Basic Input and Output
  440. Terminal Output
  441. ---------------
  442.    Since Octave normally prints the value of an expression as soon as it
  443. has been evaluated, the simplest of all I/O functions is a simple
  444. expression.  For example, the following expression will display the
  445. value of pi
  446.      pi
  447.           -| pi = 3.1416
  448.    This works well as long as it is acceptable to have the name of the
  449. variable (or `ans') printed along with the value.  To print the value
  450. of a variable without printing its name, use the function `disp'.
  451.    The `format' command offers some control over the way Octave prints
  452. values with `disp' and through the normal echoing mechanism.
  453.  - Built-in Variable: ans
  454.      This variable holds the most recently computed result that was not
  455.      explicitly assigned to a variable.  For example, after the
  456.      expression
  457.           3^2 + 4^2
  458.      is evaluated, the value of `ans' is 25.
  459.  - Built-in Function:  disp (X)
  460.      Display the value of X.  For example,
  461.           disp ("The value of pi is:"), disp (pi)
  462.           
  463.                -| the value of pi is:
  464.                -| 3.1416
  465.      Note that the output from `disp' always ends with a newline.
  466.  - Command: format OPTIONS
  467.      Control the format of the output produced by `disp' and Octave's
  468.      normal echoing mechanism.  Valid options are listed in the
  469.      following table.
  470.     `short'
  471.           Octave will try to print numbers with at least 3 significant
  472.           figures within a field that is a maximum of 8 characters wide.
  473.           If Octave is unable to format a matrix so that columns line
  474.           up on the decimal point and all the numbers fit within the
  475.           maximum field width, it switches to an `e' format.
  476.     `long'
  477.           Octave will try to print numbers with at least 15 significant
  478.           figures within a field that is a maximum of 24 characters
  479.           wide.
  480.           As will the `short' format, Octave will switch to an `e'
  481.           format if it is unable to format a matrix so that columns
  482.           line up on the decimal point and all the numbers fit within
  483.           the maximum field width.
  484.     `long e'
  485.     `short e'
  486.           The same as `format long' or `format short' but always display
  487.           output with an `e' format.  For example, with the `short e'
  488.           format, pi is displayed as `3.14e+00'.
  489.     `long E'
  490.     `short E'
  491.           The same as `format long e' or `format short e' but always
  492.           display output with an uppercase `E' format.  For example,
  493.           with the `long E' format, pi is displayed as
  494.           `3.14159265358979E+00'.
  495.     `free'
  496.     `none'
  497.           Print output in free format, without trying to line up
  498.           columns of matrices on the decimal point.  This also causes
  499.           complex numbers to be formatted like this `(0.604194,
  500.           0.607088)' instead of like this `0.60419 + 0.60709i'.
  501.     `bank'
  502.           Print in a fixed format with two places to the right of the
  503.           decimal point.
  504.     `+'
  505.           Print a `+' symbol for nonzero matrix elements and a space
  506.           for zero matrix elements.  This format can be very useful for
  507.           examining the structure of a large matrix.
  508.     `hex'
  509.           Print the hexadecimal representation numbers as they are
  510.           stored in memory.  For example, on a workstation which stores
  511.           8 byte real values in IEEE format with the least significant
  512.           byte first, the value of `pi' when printed in `hex' format is
  513.           `400921fb54442d18'.  This format only works for numeric
  514.           values.
  515.     `bit'
  516.           Print the bit representation of numbers as stored in memory.
  517.           For example, the value of `pi' is
  518.                01000000000010010010000111111011
  519.                01010100010001000010110100011000
  520.           (shown here in two 32 bit sections for typesetting purposes)
  521.           when printed in bit format on a workstation which stores 8
  522.           byte real values in IEEE format with the least significant
  523.           byte first.  This format only works for numeric types.
  524.      By default, Octave will try to print numbers with at least 5
  525.      significant figures within a field that is a maximum of 10
  526.      characters wide.
  527.      If Octave is unable to format a matrix so that columns line up on
  528.      the decimal point and all the numbers fit within the maximum field
  529.      width, it switches to an `e' format.
  530.      If `format' is invoked without any options, the default format
  531.      state is restored.
  532.  - Built-in Variable: print_answer_id_name
  533.      If the value of `print_answer_id_name' is nonzero, variable names
  534.      are printed along with the result.  Otherwise, only the result
  535.      values are printed.  The default value is 1.
  536. File: octave,  Node: Terminal Input,  Next: Simple File I/O,  Prev: Terminal Output,  Up: Basic Input and Output
  537. Terminal Input
  538. --------------
  539.    Octave has three functions that make it easy to prompt users for
  540. input.  The `input' and `menu' functions are normally used for managing
  541. an interactive dialog with a user, and the `keyboard' function is
  542. normally used for doing simple debugging.
  543.  - Built-in Function:  input (PROMPT)
  544.  - Built-in Function:  input (PROMPT, "s")
  545.      Print a prompt and wait for user input.  For example,
  546.           input ("Pick a number, any number! ")
  547.      prints the prompt
  548.           Pick a number, any number!
  549.      and waits for the user to enter a value.  The string entered by
  550.      the user is evaluated as an expression, so it may be a literal
  551.      constant, a variable name, or any other valid expression.
  552.      Currently, `input' only returns one value, regardless of the number
  553.      of values produced by the evaluation of the expression.
  554.      If you are only interested in getting a literal string value, you
  555.      can call `input' with the character string `"s"' as the second
  556.      argument.  This tells Octave to return the string entered by the
  557.      user directly, without evaluating it first.
  558.      Because there may be output waiting to be displayed by the pager,
  559.      it is a good idea to always call `fflush (stdout)' before calling
  560.      `input'.  This will ensure that all pending output is written to
  561.      the screen before your prompt.  *Note Input and Output::.
  562.  - Function File:  menu (TITLE, OPT1, ...)
  563.      Print a title string followed by a series of options.  Each option
  564.      will  be printed along with a number.  The return value is the
  565.      number of the  option selected by the user.  This function is
  566.      useful for interactive  programs.  There is no limit to the number
  567.      of options that may be passed  in, but it may be confusing to
  568.      present more than will fit easily on one  screen.
  569.  - Built-in Function:  keyboard (PROMPT)
  570.      This function is normally used for simple debugging.  When the
  571.      `keyboard' function is executed, Octave prints a prompt and waits
  572.      for user input.  The input strings are then evaluated and the
  573.      results are printed.  This makes it possible to examine the values
  574.      of variables within a function, and to assign new values to
  575.      variables.  No value is returned from the `keyboard' function, and
  576.      it continues to prompt for input until the user types `quit', or
  577.      `exit'.
  578.      If `keyboard' is invoked without any arguments, a default prompt of
  579.      `debug> ' is used.
  580.    For both `input' and `keyboard', the normal command line history and
  581. editing functions are available at the prompt.
  582.    Octave also has a function that makes it possible to get a single
  583. character from the keyboard without requiring the user to type a
  584. carriage return.
  585.  - Built-in Function:  kbhit ()
  586.      Read a single keystroke from the keyboard.  For example,
  587.           x = kbhit ();
  588.      will set X to the next character typed at the keyboard as soon as
  589.      it is typed.
  590. File: octave,  Node: Simple File I/O,  Prev: Terminal Input,  Up: Basic Input and Output
  591. Simple File I/O
  592. ---------------
  593.    The `save' and `load' commands allow data to be written to and read
  594. from disk files in various formats.  The default format of files
  595. written by the `save' command can be controlled using the built-in
  596. variables `default_save_format' and `save_precision'.
  597.    Note that Octave can not yet save or load structure variables or any
  598. user-defined types.
  599.  - Command: save OPTIONS FILE V1 V2 ...
  600.      Save the named variables V1, V2, ... in the file FILE.  The
  601.      special filename `-' can be used to write the output to your
  602.      terminal.  If no variable names are listed, Octave saves all the
  603.      variables in the current scope.  Valid options for the `save'
  604.      command are listed in the following table.  Options that modify
  605.      the output format override the format specified by the built-in
  606.      variable `default_save_format'.
  607.     `-ascii'
  608.           Save the data in Octave's text data format.
  609.     `-binary'
  610.           Save the data in Octave's binary data format.
  611.     `-float-binary'
  612.           Save the data in Octave's binary data format but only using
  613.           single precision.  You should use this format only if you
  614.           know that all the values to be saved can be represented in
  615.           single precision.
  616.     `-mat-binary'
  617.           Save the data in MATLAB's binary data format.
  618.     `-save-builtins'
  619.           Force Octave to save the values of built-in variables too.
  620.           By default, Octave does not save built-in variables.
  621.      The list of variables to save may include wildcard patterns
  622.      containing the following special characters:
  623.     `?'
  624.           Match any single character.
  625.     `*'
  626.           Match zero or more characters.
  627.     `[ LIST ]'
  628.           Match the list of characters specified by LIST.  If the first
  629.           character is `!' or `^', match all characters except those
  630.           specified by LIST.  For example, the pattern `[a-zA-Z]' will
  631.           match all lower and upper case alphabetic characters.
  632.      Except when using the MATLAB binary data file format, saving global
  633.      variables also saves the global status of the variable, so that if
  634.      it is restored at a later time using `load', it will be restored
  635.      as a global variable.
  636.      The command
  637.           save -binary data a b*
  638.      saves the variable `a' and all variables beginning with `b' to the
  639.      file `data' in Octave's binary format.
  640.    There are two variables that modify the behavior of `save' and one
  641. that controls whether variables are saved when Octave exits
  642. unexpectedly.
  643.  - Built-in Variable: crash_dumps_octave_core
  644.      If this variable is set to a nonzero value, Octave tries to save
  645.      all current variables the the file "octave-core" if it crashes or
  646.      receives a hangup, terminate or similar signal.  The default value
  647.      is 1.
  648.  - Built-in Variable: default_save_format
  649.      This variable specifies the default format for the `save' command.
  650.      It should have one of the following values: `"ascii"', `"binary"',
  651.      `float-binary', or `"mat-binary"'.  The initial default save
  652.      format is Octave's text format.
  653.  - Built-in Variable: save_precision
  654.      This variable specifies the number of digits to keep when saving
  655.      data in text format.  The default value is 17.
  656.  - Command: load OPTIONS FILE V1 V2 ...
  657.      Load the named variables from the file FILE.  As with `save', you
  658.      may specify a list of variables and `load' will only extract those
  659.      variables with names that match.  For example, to restore the
  660.      variables saved in the file `data', use the command
  661.           load data
  662.      Octave will refuse to overwrite existing variables unless you use
  663.      the option `-force'.
  664.      If a variable that is not marked as global is loaded from a file
  665.      when a global symbol with the same name already exists, it is
  666.      loaded in the global symbol table.  Also, if a variable is marked
  667.      as global in a file and a local symbol exists, the local symbol is
  668.      moved to the global symbol table and given the value from the
  669.      file.  Since it seems that both of these cases are likely to be
  670.      the result of some sort of error, they will generate warnings.
  671.      The `load' command can read data stored in Octave's text and
  672.      binary formats, and MATLAB's binary format.  It will automatically
  673.      detect the type of file and do conversion from different floating
  674.      point formats (currently only IEEE big and little endian, though
  675.      other formats may added in the future).
  676.      Valid options for `load' are listed in the following table.
  677.     `-force'
  678.           Force variables currently in memory to be overwritten by
  679.           variables with the same name found in the file.
  680.     `-ascii'
  681.           Force Octave to assume the file is in Octave's text format.
  682.     `-binary'
  683.           Force Octave to assume the file is in Octave's binary format.
  684.     `-mat-binary'
  685.           Force Octave to assume the file is in MATLAB's binary format.
  686. File: octave,  Node: C-Style I/O Functions,  Prev: Basic Input and Output,  Up: Input and Output
  687. C-Style I/O Functions
  688. =====================
  689.    Octave's C-style input and output functions provide most of the
  690. functionality of the C programming language's standard I/O library.  The
  691. argument lists for some of the input functions are slightly different,
  692. however, because Octave has no way of passing arguments by reference.
  693.    In the following, FILE refers to a file name and `fid' refers to an
  694. integer file number, as returned by `fopen'.
  695.    There are three files that are always available.  Although these
  696. files can be accessed using their corresponding numeric file ids, you
  697. should always use the symbolic names given in the table below, since it
  698. will make your programs easier to understand.
  699.  - Built-in Variable: stdin
  700.      The standard input stream (file id 0).  When Octave is used
  701.      interactively, this is filtered through the command line editing
  702.      functions.
  703.  - Built-in Variable: stdout
  704.      The standard output stream (file id 1).  Data written to the
  705.      standard output is normally filtered through the pager.
  706.  - Built-in Variable: stderr
  707.      The standard error stream (file id 2).  Even if paging is turned
  708.      on, the standard error is not sent to the pager.  It is useful for
  709.      error messages and prompts.
  710. * Menu:
  711. * Opening and Closing Files::
  712. * Simple Output::
  713. * Line-Oriented Input::
  714. * Formatted Output::
  715. * Output Conversion for Matrices::
  716. * Output Conversion Syntax::
  717. * Table of Output Conversions::
  718. * Integer Conversions::
  719. * Floating-Point Conversions::  Other Output Conversions::
  720. * Other Output Conversions::
  721. * Formatted Input::
  722. * Input Conversion Syntax::
  723. * Table of Input Conversions::
  724. * Numeric Input Conversions::
  725. * String Input Conversions::
  726. * Binary I/O::
  727. * Temporary Files::
  728. * EOF and Errors::
  729. * File Positioning::
  730. File: octave,  Node: Opening and Closing Files,  Next: Simple Output,  Prev: C-Style I/O Functions,  Up: C-Style I/O Functions
  731. Opening and Closing Files
  732. -------------------------
  733.  - Built-in Function: [FID, MSG] = fopen (NAME, MODE, ARCH)
  734.  - Built-in Function: FID_LIST = fopen ("all")
  735.  - Built-in Function: FILE = fopen (FID)
  736.      The first form of the `fopen' function opens the named file with
  737.      the specified mode (read-write, read-only, etc.) and architecture
  738.      interpretation (IEEE big endian, IEEE little endian, etc.), and
  739.      returns an integer value that may be used to refer to the file
  740.      later.  If an error occurs, FID is set to -1 and MSG contains the
  741.      corresponding system error message.  The MODE is a one or two
  742.      character string that specifies whether the file is to be opened
  743.      for reading, writing, or both.
  744.      The second form of the `fopen' function returns a vector of file
  745.      ids corresponding to all the currently open files, excluding the
  746.      `stdin', `stdout', and `stderr' streams.
  747.      The third form of the `fopen' function returns the name of a
  748.      currently open file given its file id.
  749.      For example,
  750.           myfile = fopen ("splat.dat", "r", "ieee-le");
  751.      opens the file `splat.dat' for reading.  If necessary, binary
  752.      numeric values will be read assuming they are stored in IEEE
  753.      format with the least significant bit first, and then converted to
  754.      the native representation.
  755.      Opening a file that is already open simply opens it again and
  756.      returns a separate file id.  It is not an error to open a file
  757.      several times, though writing to the same file through several
  758.      different file ids may produce unexpected results.
  759.      The possible values `mode' may have are
  760.     `r'
  761.           Open a file for reading.
  762.     `w'
  763.           Open a file for writing.  The previous contents are discared.
  764.     `a'
  765.           Open or create a file for writing at the end of the file.
  766.     `r+'
  767.           Open an existing file for reading and writing.
  768.     `w+'
  769.           Open a file for reading or writing.  The previous contents are
  770.           discarded.
  771.     `a+'
  772.           Open or create a file for reading or writing at the end of the
  773.           file.
  774.      The parameter ARCH is a string specifying the default data format
  775.      for the file.  Valid values for ARCH are:
  776.           `native' The format of the current machine (this is the
  777.           default).
  778.           `ieee-le' IEEE big endian format.
  779.           `ieee-be' IEEE little endian format.
  780.           `vaxd' VAX D floating format.
  781.           `vaxg' VAX G floating format.
  782.           `cray' Cray floating format.
  783.      however, conversions are currently only supported for `native'
  784.      `ieee-be', and `ieee-le' formats.
  785.  - Built-in Function:  fclose (FID)
  786.      Closes the specified file.  If an error is encountered while
  787.      trying to close the file, an error message is printed and `fclose'
  788.      returns 0.  Otherwise, it returns 1.
  789. File: octave,  Node: Simple Output,  Next: Line-Oriented Input,  Prev: Opening and Closing Files,  Up: C-Style I/O Functions
  790. Simple Output
  791. -------------
  792.  - Built-in Function:  fputs (FID, STRING)
  793.      Write a string to a file with no formatting.
  794.  - Function File:  puts (STRING)
  795.      Write a string to the standard output with no formatting.
  796. File: octave,  Node: Line-Oriented Input,  Next: Formatted Output,  Prev: Simple Output,  Up: C-Style I/O Functions
  797. Line-Oriented Input
  798. -------------------
  799.  - Built-in Function:  fgetl (FID, LEN)
  800.      Read characters from a file, stopping after a newline, or EOF, or
  801.      LEN characters have been read.  The characters read, excluding the
  802.      possible trailing newline, are returned as a string.
  803.      If LEN is omitted, `fgetl' reads until the next newline character.
  804.      If there are no more characters to read, `fgetl' returns -1.
  805.  - Built-in Function:  fgets (FID, LEN)
  806.      Read characters from a file, stopping after a newline, or EOF, or
  807.      LEN characters have been read.  The characters read, including the
  808.      possible trailing newline, are returned as a string.
  809.      If LEN is omitted, `fgets' reads until the next newline character.
  810.      If there are no more characters to read, `fgets' returns -1.
  811. File: octave,  Node: Formatted Output,  Next: Output Conversion for Matrices,  Prev: Line-Oriented Input,  Up: C-Style I/O Functions
  812. Formatted Output
  813. ----------------
  814.    This section describes how to call `printf' and related functions.
  815.    The following functions are available for formatted output.  They are
  816. modelled after the C language functions of the same name, but they
  817. interpret the format template differently in order to improve the
  818. performance of printing vector and matrix values.
  819.  - Function File:  printf (TEMPLATE, ...)
  820.      The `printf' function prints the optional arguments under the
  821.      control of the template string TEMPLATE to the stream `stdout'.
  822.  - Built-in Function:  fprintf (FID, TEMPLATE, ...)
  823.      This function is just like `printf', except that the output is
  824.      written to the stream FID instead of `stdout'.
  825.  - Built-in Function:  sprintf (TEMPLATE, ...)
  826.      This is like `printf', except that the output is returned as a
  827.      string.  Unlike the C library function, which requires you to
  828.      provide a suitably sized string as an argument, Octave's `sprintf'
  829.      function returns the string, automatically sized to hold all of
  830.      the items converted.
  831.    The `printf' function can be used to print any number of arguments.
  832. The template string argument you supply in a call provides information
  833. not only about the number of additional arguments, but also about their
  834. types and what style should be used for printing them.
  835.    Ordinary characters in the template string are simply written to the
  836. output stream as-is, while "conversion specifications" introduced by a
  837. `%' character in the template cause subsequent arguments to be
  838. formatted and written to the output stream.  For example,
  839.      pct = 37;
  840.      filename = "foo.txt";
  841.      printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
  842.              filename, pct);
  843. produces output like
  844.      Processing of `foo.txt' is 37% finished.
  845.      Please be patient.
  846.    This example shows the use of the `%d' conversion to specify that a
  847. scalar argument should be printed in decimal notation, the `%s'
  848. conversion to specify printing of a string argument, and the `%%'
  849. conversion to print a literal `%' character.
  850.    There are also conversions for printing an integer argument as an
  851. unsigned value in octal, decimal, or hexadecimal radix (`%o', `%u', or
  852. `%x', respectively); or as a character value (`%c').
  853.    Floating-point numbers can be printed in normal, fixed-point notation
  854. using the `%f' conversion or in exponential notation using the `%e'
  855. conversion.  The `%g' conversion uses either `%e' or `%f' format,
  856. depending on what is more appropriate for the magnitude of the
  857. particular number.
  858.    You can control formatting more precisely by writing "modifiers"
  859. between the `%' and the character that indicates which conversion to
  860. apply.  These slightly alter the ordinary behavior of the conversion.
  861. For example, most conversion specifications permit you to specify a
  862. minimum field width and a flag indicating whether you want the result
  863. left- or right-justified within the field.
  864.    The specific flags and modifiers that are permitted and their
  865. interpretation vary depending on the particular conversion.  They're all
  866. described in more detail in the following sections.
  867. File: octave,  Node: Output Conversion for Matrices,  Next: Output Conversion Syntax,  Prev: Formatted Output,  Up: C-Style I/O Functions
  868. Output Conversion for Matrices
  869. ------------------------------
  870.    When given a matrix value, Octave's formatted output functions cycle
  871. through the format template until all the values in the matrix have been
  872. printed.  For example,
  873.      printf ("%4.2f %10.2e %8.4g\n", hilb (3));
  874.      
  875.           -| 1.00   5.00e-01   0.3333
  876.           -| 0.50   3.33e-01     0.25
  877.           -| 0.33   2.50e-01      0.2
  878.    If more than one value is to be printed in a single call, the output
  879. functions do not return to the beginning of the format template when
  880. moving on from one value to the next.  This can lead to confusing output
  881. if the number of elements in the matrices are not exact multiples of the
  882. number of conversions in the format template.  For example,
  883.      printf ("%4.2f %10.2e %8.4g\n", [1, 2], [3, 4]);
  884.      
  885.           -| 1.00   2.00e+00        3
  886.           -| 4.00
  887.    If this is not what you want, use a series of calls instead of just
  888. File: octave,  Node: Output Conversion Syntax,  Next: Table of Output Conversions,  Prev: Output Conversion for Matrices,  Up: C-Style I/O Functions
  889. Output Conversion Syntax
  890. ------------------------
  891.    This section provides details about the precise syntax of conversion
  892. specifications that can appear in a `printf' template string.
  893.    Characters in the template string that are not part of a conversion
  894. specification are printed as-is to the output stream.
  895.    The conversion specifications in a `printf' template string have the
  896. general form:
  897.      % FLAGS WIDTH [ . PRECISION ] TYPE CONVERSION
  898.    For example, in the conversion specifier `%-10.8ld', the `-' is a
  899. flag, `10' specifies the field width, the precision is `8', the letter
  900. `l' is a type modifier, and `d' specifies the conversion style.  (This
  901. particular type specifier says to print a numeric argument in decimal
  902. notation, with a minimum of 8 digits left-justified in a field at least
  903. 10 characters wide.)
  904.    In more detail, output conversion specifications consist of an
  905. initial `%' character followed in sequence by:
  906.    * Zero or more "flag characters" that modify the normal behavior of
  907.      the conversion specification.
  908.    * An optional decimal integer specifying the "minimum field width".
  909.      If the normal conversion produces fewer characters than this, the
  910.      field is padded with spaces to the specified width.  This is a
  911.      *minimum* value; if the normal conversion produces more characters
  912.      than this, the field is *not* truncated.  Normally, the output is
  913.      right-justified within the field.
  914.      You can also specify a field width of `*'.  This means that the
  915.      next argument in the argument list (before the actual value to be
  916.      printed) is used as the field width.  The value is rounded to the
  917.      nearest integer.  If the value is negative, this means to set the
  918.      `-' flag (see below) and to use the absolute value as the field
  919.      width.
  920.    * An optional "precision" to specify the number of digits to be
  921.      written for the numeric conversions.  If the precision is
  922.      specified, it consists of a period (`.') followed optionally by a
  923.      decimal integer (which defaults to zero if omitted).
  924.      You can also specify a precision of `*'.  This means that the next
  925.      argument in the argument list (before the actual value to be
  926.      printed) is used as the precision.  The value must be an integer,
  927.      and is ignored if it is negative.
  928.    * An optional "type modifier character".  This character is ignored
  929.      by Octave's `printf' function, but is recognized to provide
  930.      compatibility with the C language `printf'.
  931.    * A character that specifies the conversion to be applied.
  932.    The exact options that are permitted and how they are interpreted
  933. vary between the different conversion specifiers.  See the descriptions
  934. of the individual conversions for information about the particular
  935. options that they use.
  936. File: octave,  Node: Table of Output Conversions,  Next: Integer Conversions,  Prev: Output Conversion Syntax,  Up: C-Style I/O Functions
  937. Table of Output Conversions
  938. ---------------------------
  939.    Here is a table summarizing what all the different conversions do:
  940. `%d', `%i'
  941.      Print an integer as a signed decimal number.  *Note Integer
  942.      Conversions::, for details.  `%d' and `%i' are synonymous for
  943.      output, but are different when used with `scanf' for input (*note
  944.      Table of Input Conversions::.).
  945.      Print an integer as an unsigned octal number.  *Note Integer
  946.      Conversions::, for details.
  947.      Print an integer as an unsigned decimal number.  *Note Integer
  948.      Conversions::, for details.
  949. `%x', `%X'
  950.      Print an integer as an unsigned hexadecimal number.  `%x' uses
  951.      lower-case letters and `%X' uses upper-case.  *Note Integer
  952.      Conversions::, for details.
  953.      Print a floating-point number in normal (fixed-point) notation.
  954.      *Note Floating-Point Conversions::, for details.
  955. `%e', `%E'
  956.      Print a floating-point number in exponential notation.  `%e' uses
  957.      lower-case letters and `%E' uses upper-case.  *Note Floating-Point
  958.      Conversions::, for details.
  959. `%g', `%G'
  960.      Print a floating-point number in either normal (fixed-point) or
  961.      exponential notation, whichever is more appropriate for its
  962.      magnitude.  `%g' uses lower-case letters and `%G' uses upper-case.
  963.      *Note Floating-Point Conversions::, for details.
  964.      Print a single character.  *Note Other Output Conversions::.
  965.      Print a string.  *Note Other Output Conversions::.
  966.      Print a literal `%' character.  *Note Other Output Conversions::.
  967.    If the syntax of a conversion specification is invalid, unpredictable
  968. things will happen, so don't do this.  If there aren't enough function
  969. arguments provided to supply values for all the conversion
  970. specifications in the template string, or if the arguments are not of
  971. the correct types, the results are unpredictable.  If you supply more
  972. arguments than conversion specifications, the extra argument values are
  973. simply ignored; this is sometimes useful.
  974.