For loop to enter values in array inputted by user.
1 次查看(过去 30 天)
显示 更早的评论
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
2023-12-21
x = ' ';
isnumeric(x)
Thus the
xn(i) = x;
part of your code should never be executed.
回答(3 个)
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.
0 个评论
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
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!