For loop to enter values in array inputted by user.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Torsten
le 21 Déc 2023
x = ' ';
isnumeric(x)
Thus the
xn(i) = x;
part of your code should never be executed.
Réponses (3)
Hassaan
le 21 Déc 2023
Modifié(e) : Hassaan
le 22 Déc 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.
0 commentaires
Walter Roberson
le 21 Déc 2023
Modifié(e) : Walter Roberson
le 21 Déc 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
0 commentaires
Sulaymon Eshkabilov
le 22 Déc 2023
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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!