필터 지우기
필터 지우기

Store numbers in an array from user input

조회 수: 31 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by