Check input is a string or Char if so reprompt
    4 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
I am struggling to fidn a way to check a input if its any type of string or char and if so reprompt the user.
Code is below:
a = input("Please enter a number: "); %Takes user input for number that is to be converted
while isempty(a)
    disp('Invalid input please enter a number!')
    a = input("Please enter a number: ");
end
convtype = input("Please enter the conversion type: "); %Takes user input for the cnversion type that they want
while isempty(convtype)|| convtype < 1 || convtype > 14
    disp('Invalid conversion type selection please make a selection between 1 and 14!')
    convtype = input("Please enter the conversion type: ");
end
2 commentaires
  Stephen23
      
      
 le 5 Mai 2024
				If you specify the 's' option then INPUT will always return a character vector:
txt = input(prompt,'s')
Réponses (1)
  Walter Roberson
      
      
 le 6 Mai 2024
        a = input("Please enter a number: "); %Takes user input for number that is to be converted
while ~isnumeric(a) || numel(a) ~= 1
    disp('Invalid input please enter a number!')
    a = input("Please enter a number: ");
end
convtype = input("Please enter the conversion type, integer 1 to 14: "); %Takes user input for the cnversion type that they want
while ~isnumeric(convtype) || numel(convtype) ~= 1 || convtype ~= floor(convtype) || convtype < 1 || convtype > 14
    disp('Invalid conversion type selection please make a selection between 1 and 14!')
    convtype = input("Please enter the conversion type as an integer 1 to 14: ");
end
3 commentaires
  Dyuman Joshi
      
      
 le 6 Mai 2024
				~ is the logical not operator -  not, ~
~= is ne, ~=
Voir également
Catégories
				En savoir plus sur Characters and Strings dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



