输入和数组 [英] input and arrays
问题描述
我想通过只问一次问题,像这样的输出,来知道如何在MATLAB中的数组中存储输入值
I want to know how to store input values in an array in MATLAB by asking the question only one time, like this output
输入质量(mo)值:[1 2 3 4 5]
Enter values for mass (mo): [1 2 3 4 5]
推荐答案
The default behavior of INPUT will allow you to enter a vector, provided you include the square brackets when typing your input:
>> vec = input('Enter values for mass (mo): '); %# Ask for input
Enter values for mass (mo): [1 2 3 4 5] %# Enter [1 2 3 4 5]
>> vec %# Display vec
vec =
1 2 3 4 5
这种行为可能并不明显,因为您可能习惯于使用输入.您必须特别注意文档的这一部分(强调):
This behavior may not be obvious, as you may be used to entering only scalar values and strings with INPUT. You have to pay special attention to this part of the documentation (emphasis added):
显示
evalResponse = input(prompt)
屏幕上的prompt
字符串,等待 对于键盘输入,求值 输入中的所有表达式,然后 返回evalResponse
中的值.到 计算表达式,input
函数访问变量 当前的工作区.
evalResponse = input(prompt)
displays theprompt
string on the screen, waits for input from the keyboard, evaluates any expressions in the input, and returns the value inevalResponse
. To evaluate expressions, theinput
function accesses variables in the current workspace.
这意味着您甚至可以输入以下内容:
This means you could even enter input like this:
Enter values for mass (mo): [1*8 2+9 zeros(1,3) pi]
>> vec
vec =
8.0000 11.0000 0 0 0 3.1416
这篇关于输入和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!