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 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Terminal Input (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Terminal Input (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Terminal Input (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Basic-Input-and-Output.html" rel="up" title="Basic Input and Output">
<link href="Simple-File-I_002fO.html" rel="next" title="Simple File I/O">
<link href="Terminal-Output.html" rel="prev" title="Terminal Output">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Terminal-Input">
<div class="nav-panel">
<p>
Next: <a href="Simple-File-I_002fO.html" accesskey="n" rel="next">Simple File I/O</a>, Previous: <a href="Terminal-Output.html" accesskey="p" rel="prev">Terminal Output</a>, Up: <a href="Basic-Input-and-Output.html" 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" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Terminal-Input-1"><span>14.1.2 Terminal Input<a class="copiable-link" href="#Terminal-Input-1"> ¶</a></span></h4>
<p>Octave has three functions that make it easy to prompt users for
input. The <code class="code">input</code> and <code class="code">menu</code> functions are normally
used for managing an interactive dialog with a user, and the
<code class="code">keyboard</code> function is normally used for doing simple debugging.
</p>
<a class="anchor" id="XREFinput"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-input"><span><code class="def-type"><var class="var">ans</var> =</code> <strong class="def-name">input</strong> <code class="def-code-arguments">(<var class="var">prompt</var>)</code><a class="copiable-link" href="#index-input"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-input-1"><span><code class="def-type"><var class="var">ans</var> =</code> <strong class="def-name">input</strong> <code class="def-code-arguments">(<var class="var">prompt</var>, "s")</code><a class="copiable-link" href="#index-input-1"> ¶</a></span></dt>
<dd><p>Print <var class="var">prompt</var> and wait for user input.
</p>
<p>For example,
</p>
<div class="example">
<pre class="example-preformatted">input ("Pick a number, any number! ")
</pre></div>
<p>prints the prompt
</p>
<div class="example">
<pre class="example-preformatted">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 Octave code.
</p>
<p>The number of return arguments, their size, and their class depend on the
expression entered.
</p>
<p>If you are only interested in getting a literal string value, you can call
<code class="code">input</code> with the character string <code class="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 class="code">fflush (stdout)</code> before calling <code class="code">input</code>.
This will ensure that all pending output is written to the screen before
your prompt.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFyes_005for_005fno">yes_or_no</a>, <a class="ref" href="#XREFkbhit">kbhit</a>, <a class="ref" href="Timing-Utilities.html#XREFpause">pause</a>, <a class="ref" href="#XREFmenu">menu</a>, <a class="ref" href="I_002fO-Dialogs.html#XREFlistdlg">listdlg</a>.
</p></dd></dl>
<a class="anchor" id="XREFmenu"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-menu"><span><code class="def-type"><var class="var">choice</var> =</code> <strong class="def-name">menu</strong> <code class="def-code-arguments">(<var class="var">title</var>, <var class="var">opt1</var>, …)</code><a class="copiable-link" href="#index-menu"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-menu-1"><span><code class="def-type"><var class="var">choice</var> =</code> <strong class="def-name">menu</strong> <code class="def-code-arguments">(<var class="var">title</var>, {<var class="var">opt1</var>, …})</code><a class="copiable-link" href="#index-menu-1"> ¶</a></span></dt>
<dd><p>Display a menu with heading <var class="var">title</var> and options <var class="var">opt1</var>, …,
and wait for user input.
</p>
<p>If the GUI is running, the menu is displayed graphically using
<code class="code">listdlg</code>. Otherwise, the title and menu options are printed on the
console.
</p>
<p><var class="var">title</var> is a string and the options may be input as individual strings
or as a cell array of strings.
</p>
<p>The return value <var class="var">choice</var> is the number of the option selected by the
user counting from 1. If the user aborts the dialog or makes an invalid
selection then 0 is returned.
</p>
<p>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 class="strong">See also:</strong> <a class="ref" href="#XREFinput">input</a>, <a class="ref" href="I_002fO-Dialogs.html#XREFlistdlg">listdlg</a>.
</p></dd></dl>
<a class="anchor" id="XREFyes_005for_005fno"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-yes_005for_005fno"><span><code class="def-type"><var class="var">ans</var> =</code> <strong class="def-name">yes_or_no</strong> <code class="def-code-arguments">("<var class="var">prompt</var>")</code><a class="copiable-link" href="#index-yes_005for_005fno"> ¶</a></span></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.
</p>
<p>Takes one argument, <var class="var">prompt</var>, which is the string to display when asking
the question. <var class="var">prompt</var> should end in a space; <code class="code">yes-or-no</code> adds the
string ‘<samp class="samp">(yes or no) </samp>’ to it. The user must confirm the answer with
<kbd class="key">RET</kbd> and can edit it until it has been confirmed.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFinput">input</a>.
</p></dd></dl>
<p>For <code class="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 class="anchor" id="XREFkbhit"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-kbhit"><span><code class="def-type"><var class="var">c</var> =</code> <strong class="def-name">kbhit</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-kbhit"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-kbhit-1"><span><code class="def-type"><var class="var">c</var> =</code> <strong class="def-name">kbhit</strong> <code class="def-code-arguments">(1)</code><a class="copiable-link" href="#index-kbhit-1"> ¶</a></span></dt>
<dd><p>Read a single keystroke from the keyboard.
</p>
<p>If called with an argument (typically 1), don’t wait for a keypress and
immediately return the next keystroke in the keyboard input buffer or an empty
string ("") if no keystroke is available.
</p>
<p>For example,
</p>
<div class="example">
<pre class="example-preformatted">c = kbhit ();
</pre></div>
<p>will set <var class="var">c</var> to the next character typed at the keyboard as soon as it is
typed.
</p>
<div class="example">
<pre class="example-preformatted">c = 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 class="strong">See also:</strong> <a class="ref" href="#XREFinput">input</a>, <a class="ref" href="Timing-Utilities.html#XREFpause">pause</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Simple-File-I_002fO.html">Simple File I/O</a>, Previous: <a href="Terminal-Output.html">Terminal Output</a>, Up: <a href="Basic-Input-and-Output.html">Basic Input and Output</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|