帮助中心帮助中心
请求用户输入
x = input(prompt)
txt = input(prompt,"s")
示例
x = input(prompt) 显示 prompt 中的文本并等待用户输入值后按 Return 键。用户可以输入 pi/4 或 rand(3) 之类的表达式,并可以使用工作区中的变量。
x
prompt
pi/4
rand(3)
如果用户不输入任何内容直接按下 Return 键,则 input 会返回空矩阵。
input
如果用户在提示下输入无效的表达式,则 MATLAB® 会显示相关的错误消息,然后重新显示提示。
txt = input(prompt,"s") 返回输入的文本,而不会将输入作为表达式来计算。
txt
全部折叠
请求一个数值输入,然后将该输入乘以 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]: "; txt = input(prompt,"s"); if isempty(txt) txt = 'Y'; end
input 函数返回与键入内容完全相同的文本。如果输入为空,此代码将为 txt 指定默认值 'Y'。
'Y'
显示给用户的文本,指定为字符串或字符向量。
要创建跨越多行的提示,请使用 \n 指示每个新行。要在提示中包含反斜杠 (\),请使用 \\。
\n
\
\\
根据输入计算的结果,以数组形式返回。数组的类型和维度取决于对提示的响应。
确切的输入文本,以字符向量形式返回。
当 input 等待用户响应时,工作区浏览器不会刷新。因此,如果您在脚本中运行 input,则在脚本运行完之前,工作区浏览器不会显示对工作区中的变量所做的更改。
在 R2006a 之前推出
keyboard | inputdlg | listdlg | ginput | uicontrol
keyboard
inputdlg
listdlg
ginput
uicontrol
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
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