1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Terminal Input</title>
<meta name="description" content="GNU Octave: Terminal Input">
<meta name="keywords" content="GNU Octave: Terminal Input">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Basic-Input-and-Output.html#Basic-Input-and-Output" rel="up" title="Basic Input and Output">
<link href="Simple-File-I_002fO.html#Simple-File-I_002fO" rel="next" title="Simple File I/O">
<link href="Paging-Screen-Output.html#Paging-Screen-Output" rel="prev" title="Paging Screen Output">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Terminal-Input"></a>
<div class="header">
<p>
Next: <a href="Simple-File-I_002fO.html#Simple-File-I_002fO" accesskey="n" rel="next">Simple File I/O</a>, Previous: <a href="Terminal-Output.html#Terminal-Output" accesskey="p" rel="prev">Terminal Output</a>, Up: <a href="Basic-Input-and-Output.html#Basic-Input-and-Output" accesskey="u" rel="up">Basic Input and Output</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Terminal-Input-1"></a>
<h4 class="subsection">14.1.2 Terminal Input</h4>
<p>Octave has three functions that make it easy to prompt users for
input. The <code>input</code> and <code>menu</code> functions are normally
used for managing an interactive dialog with a user, and the
<code>keyboard</code> function is normally used for doing simple debugging.
</p>
<a name="XREFinput"></a><dl>
<dt><a name="index-input"></a>Built-in Function: <em><var>ans</var> =</em> <strong>input</strong> <em>(<var>prompt</var>)</em></dt>
<dt><a name="index-input-1"></a>Built-in Function: <em><var>ans</var> =</em> <strong>input</strong> <em>(<var>prompt</var>, "s")</em></dt>
<dd><p>Print <var>prompt</var> and wait for user input.
</p>
<p>For example,
</p>
<div class="example">
<pre class="example">input ("Pick a number, any number! ")
</pre></div>
<p>prints the prompt
</p>
<div class="example">
<pre class="example">Pick a number, any number!
</pre></div>
<p>and waits for the user to enter a value. The string entered by the user
is evaluated as an expression, so it may be a literal constant, a
variable name, or any other valid expression.
</p>
<p>Currently, <code>input</code> only returns one value, regardless of the number
of values produced by the evaluation of the expression.
</p>
<p>If you are only interested in getting a literal string value, you can
call <code>input</code> with the character string <code>"s"</code> as the second
argument. This tells Octave to return the string entered by the user
directly, without evaluating it first.
</p>
<p>Because there may be output waiting to be displayed by the pager, it is
a good idea to always call <code>fflush (stdout)</code> before calling
<code>input</code>. This will ensure that all pending output is written to
the screen before your prompt.
</p>
<p><strong>See also:</strong> <a href="#XREFyes_005for_005fno">yes_or_no</a>, <a href="#XREFkbhit">kbhit</a>, <a href="Timing-Utilities.html#XREFpause">pause</a>, <a href="#XREFmenu">menu</a>, <a href="Dialog-Box-Functions.html#XREFlistdlg">listdlg</a>.
</p></dd></dl>
<a name="XREFmenu"></a><dl>
<dt><a name="index-menu"></a>Function File: <em></em> <strong>menu</strong> <em>(<var>title</var>, <var>opt1</var>, …)</em></dt>
<dd><p>Print a title string followed by a series of options. Each option will
be printed along with a number. The return value is the number of the
option selected by the user. This function is useful for interactive
programs. There is no limit to the number of options that may be passed
in, but it may be confusing to present more than will fit easily on one
screen.
</p>
<p><strong>See also:</strong> <a href="#XREFinput">input</a>, <a href="Dialog-Box-Functions.html#XREFlistdlg">listdlg</a>.
</p></dd></dl>
<a name="XREFyes_005for_005fno"></a><dl>
<dt><a name="index-yes_005for_005fno"></a>Built-in Function: <em><var>ans</var> =</em> <strong>yes_or_no</strong> <em>("<var>prompt</var>")</em></dt>
<dd><p>Ask the user a yes-or-no question.
</p>
<p>Return logical true if the answer is yes or false if the answer is no.
Takes one argument, <var>prompt</var>, which is the string to display when asking
the question. <var>prompt</var> should end in a space; <code>yes-or-no</code> adds the
string ‘<samp>(yes or no) </samp>’ to it. The user must confirm the answer with
<tt class="key">RET</tt> and can edit it until it has been confirmed.
</p>
<p><strong>See also:</strong> <a href="#XREFinput">input</a>.
</p></dd></dl>
<p>For <code>input</code>, the normal command line history and editing functions
are available at the prompt.
</p>
<p>Octave also has a function that makes it possible to get a single
character from the keyboard without requiring the user to type a
carriage return.
</p>
<a name="XREFkbhit"></a><dl>
<dt><a name="index-kbhit"></a>Built-in Function: <em></em> <strong>kbhit</strong> <em>()</em></dt>
<dt><a name="index-kbhit-1"></a>Built-in Function: <em></em> <strong>kbhit</strong> <em>(1)</em></dt>
<dd><p>Read a single keystroke from the keyboard. If called with an
argument, don’t wait for a keypress. For example,
</p>
<div class="example">
<pre class="example">x = kbhit ();
</pre></div>
<p>will set <var>x</var> to the next character typed at the keyboard as soon as
it is typed.
</p>
<div class="example">
<pre class="example">x = kbhit (1);
</pre></div>
<p>is identical to the above example, but doesn’t wait for a keypress,
returning the empty string if no key is available.
</p>
<p><strong>See also:</strong> <a href="#XREFinput">input</a>, <a href="Timing-Utilities.html#XREFpause">pause</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Simple-File-I_002fO.html#Simple-File-I_002fO" accesskey="n" rel="next">Simple File I/O</a>, Previous: <a href="Terminal-Output.html#Terminal-Output" accesskey="p" rel="prev">Terminal Output</a>, Up: <a href="Basic-Input-and-Output.html#Basic-Input-and-Output" accesskey="u" rel="up">Basic Input and Output</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|