For loop to enter values in array inputted by user.

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 comentarios

Torsten
Torsten el 21 de Dic. de 2023
Editada: Torsten el 21 de Dic. de 2023
If the user enters a non-numeric value, the loop will terminate (e.g. by inputting a blank space as you suggest in the input message to the user).
Anthony
Anthony el 21 de Dic. de 2023
Editada: Anthony el 21 de Dic. de 2023
So, the problem is, x becomes an emtpy array, and this comes up when inputting a blank space
Unable to perform assignment because the left and right sides have a different number of elements.
Error in ExtraCredit (line 6)
xn(i) = x;
x = ' ';
isnumeric(x)
ans = logical
0
Thus the
xn(i) = x;
part of your code should never be executed.
Anthony
Anthony el 21 de Dic. de 2023
Putting x = ' ' before the if statement terminates the code after the first numercal value is inputted.

Iniciar sesión para comentar.

Respuestas (3)

Hassaan
Hassaan el 21 de Dic. de 2023
Editada: Hassaan el 22 de Dic. de 2023
% 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 el 21 de Dic. de 2023
Editada: Walter Roberson el 21 de Dic. de 2023
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
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

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 21 de Dic. de 2023

Respondida:

el 22 de Dic. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by