【matlab】数组赋值,用户输入
for i=1:5
x(i)=input('输入元素');
end;
老师给了批注,如下:
x=input('vector_');
难道可以直接一次性输入数组的5个元素吗?我试了几次,没有试出来。。。
请大家帮帮我呀。 展开
可能直接用:
x=input('输入元素:','s');
MATLAB帮助文件:
input:
Request user input
collapse all in page
Syntax
x = input(prompt)
example
str = input(prompt,'s')
example
If the user presses the Return key without entering anything, then input returns an empty matrix.
If the user enters an invalid expression at the prompt, then MATLAB® displays the relevant error message, and then redisplays the prompt.
- prompt = 'What is the original value? ';
- x = input(prompt)
- y = x*10
- x =
- 42
- y =
- 420
- prompt = 'What is the original value? ';
- x = input(prompt)
- y = x*10
- 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
Description
example
x = input(prompt) displays the text in prompt and waits for the user to input a value and press the Return key. The user can enter expressions, like pi/4 or rand(3), and can use variables in the workspace.
example
str = input(prompt,'s') returns the entered text as a string, without evaluating the input as an expression.
Examples
collapse all
Request Numeric Input or Expression
Request a numeric input, and then multiply the input by 10.
At the prompt, enter a numeric value or array, such as 42.
The input function also accepts expressions. For example, rerun the code.
At the prompt, enter magic(3).
Request Unprocessed Text Input
Request a simple text response that requires no evaluation.
The input function returns the text exactly as typed. If the input is empty, this code assigns a default value, 'Y', to the output string, str.
Input Arguments
collapse all
prompt — Text displayed to the userstring
Text displayed to the user, specified as a string.
To create a prompt that spans several lines, use '\n' to indicate each new line. To include a backslash ('\') in the prompt, use '\\'.
Output Arguments
collapse all
x — Result calculated from inputarray
Result calculated from input, returned as an array. The type and dimensions of the array depend upon the response to the prompt.
str — Exact text of inputstring
Exact text of the input, returned as a string.
str = input(prompt,'s') returns the entered text as a string, without evaluating the input as an expression.