Store numbers in an array from user input

91 ビュー (過去 30 日間)
Student
Student 2018 年 3 月 28 日
コメント済み: Stephen23 2021 年 6 月 9 日
How would I store numbers into an array one at a time from a user input? Here is what I have, but I'm getting an error saying
count = 0;
while count < 10
x(count) = input('Input a number: ');
count = count + 1;
end
x = x(1:count);
Subscript indices must either be real positive integers or logicals.
Error in untitled2 (line 5) x(count) = input('Input a number: ')
  5 件のコメント
Rik
Rik 2020 年 8 月 25 日
An array is a much better idea. That way you don't need to generate the variable name later in your code when you want to use the values. Using an array also means you can easily count how many variables were entered, as you can just count the elements with numel.
Md Iqbal
Md Iqbal 2020 年 10 月 24 日
Store string in array from user input in matlab GUI

サインインしてコメントする。

回答 (1 件)

Shivani Dixit
Shivani Dixit 2021 年 6 月 9 日
In the above given code it is also possible to form the array while taking the input from the user.
Also in the below code given for reference, you need not worry about the count as it is not used for indexing and only for incrementing each time a value is given until upper limit (here, 10)
You can refer to the below code for the same :
count = 0;
x=[];
while count < 10
y = input('Input a number: ');
x=[x y];
count = count + 1;
end
x % prints out the array containing values given by user

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by