文档
本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。
请求用户输入
x = input(prompt)
str = input(prompt,'s')
示例
x = input(prompt) 显示 prompt 中的文本并等待用户输入值后按 Return 键。用户可以输入 pi/4 或 rand(3) 之类的表达式,并可以使用工作区中的变量。
x
prompt
pi/4
rand(3)
如果用户不输入任何内容直接按下 Return 键,则 input 会返回空矩阵。
input
如果用户在提示下输入无效的表达式,则 MATLAB® 会显示相关的错误消息,然后重新显示提示。
str = input(prompt,'s') 返回输入的文本,而不会将输入作为表达式来计算。
str
全部折叠
请求一个数值输入,然后将该输入乘以 10。
prompt = 'What is the original value? '; x = input(prompt) y = x*10
在提示下,输入一个数值或数组(如 42)。
42
x = 42 y = 420
input 函数还接受表达式。例如,重新运行以下代码。
在提示下,输入 magic(3)。
magic(3)
x = 8 1 6 3 5 7 4 9 2 y = 80 10 60 30 50 70 40 90 20
请求一个简单的不需要计算的文本响应。
prompt = 'Do you want more? Y/N [Y]: '; str = input(prompt,'s'); if isempty(str) str = 'Y'; end
input 函数返回与键入内容完全相同的文本。如果输入为空,此代码将为 str 指定默认值 'Y'。
'Y'
显示给用户的文本,指定为字符向量。
要创建跨越多行的提示,请使用 '\n' 指示每个新行。要在提示中包含反斜杠 ('\'),请使用 '\\'。
'\n'
'\'
'\\'
根据输入计算的结果,以数组形式返回。数组的类型和维度取决于对提示的响应。
确切的输入文本,以字符向量形式返回。
当 input 等待用户响应时,工作区浏览器不会刷新。因此,如果您在脚本中运行 input,则在脚本运行完之前,工作区浏览器不会显示对工作区中的变量所做的更改。
ginput | inputdlg | keyboard | menu | uicontrol
ginput
inputdlg
keyboard
menu
uicontrol
您点击了调用以下 MATLAB 命令的链接:
Web 浏览器不支持 MATLAB 命令。请在 MATLAB 命令窗口中直接输入该命令以运行它。
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office