For loop to enter values in array inputted by user.

조회 수: 4 (최근 30일)
Anthony
Anthony 2023년 12월 21일
답변: Sulaymon Eshkabilov 2023년 12월 22일
for i = 1:inf
x = input('Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished. ');
if isnumeric(x)
xn(i) = x;
else
break
end
end
I want the user to enter numbers into an array, xn, one by one. The creation of xn array works, but I want them to be able to terminate it when they are done. How do I do this?
  댓글 수: 4
Torsten
Torsten 2023년 12월 21일
x = ' ';
isnumeric(x)
ans = logical
0
Thus the
xn(i) = x;
part of your code should never be executed.
Anthony
Anthony 2023년 12월 21일
Putting x = ' ' before the if statement terminates the code after the first numercal value is inputted.

댓글을 달려면 로그인하십시오.

답변 (3개)

Hassaan
Hassaan 2023년 12월 21일
편집: Hassaan 2023년 12월 22일
% Initialize the array
xn = [];
% Start an infinite loop
while true
x = input('Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished: ', 's');
% Check if the input is a blank space or is not a numuric value, then break the loop
if isempty(x) || ~isnumeric(x)
break;
else
% Convert the string to a number
x = str2double(x);
% Check if the conversion was successful, i.e., x is a number
if ~isnan(x)
% Append the number to the xn array
xn(end+1) = x;
else
% If input was not a number, prompt again
disp('Please enter a numeric value.');
end
end
end
% Display the final array
disp('The components of the signal are:');
disp(xn);
In this code:
  • An infinite while loop is used instead of a for loop.
  • input function is called with the 's' flag to read the input as a string, allowing for the detection of a blank space.
  • The isempty function checks if the user entered a blank space to exit the loop.
  • The str2double function is used to attempt to convert the string input to a numeric value. If the conversion fails (user enters non-numeric input), str2double returns NaN, which can be checked using isnan.
  • If the input is numeric and not NaN, the number is appended to the xn array.
  • If the user enters a blank space (just presses Enter), the loop breaks.
This allows you to keep entering numbers that will be stored in the xn array, and when finished, you can simply press Enter without typing anything to terminate the input process.
Output
Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished: 1
Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished: 2
Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished: 3
Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished: 4
Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished: 5
Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished:
The components of the signal are:
1 2 3 4 5
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

Walter Roberson
Walter Roberson 2023년 12월 21일
편집: Walter Roberson 2023년 12월 21일
i = 0;
while true
x = input('Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished. ');
if isempty(x) || ~isnumeric(x)
break
elseif ~isscalar(x)
fprintf('Enter only one value at a time\n');
else
i = i + 1;
xn(i) = x;
end
end

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 12월 22일
Here is a bit sipler solution:
x = 0;
xn = [];
while ~isempty(x)
x = input('Insert the X components of signal (INCLUDING ZEROS) one by one, clicking enter after each input. Enter a blank space when finished. ');
xn=[xn,x];
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by