Character exclusion using ischar() in script.

3 次查看(过去 30 天)
Any idea how to exclude characters from the inputs using ischar() ? Every try I made has failed. I want to show user a message when he/she types letters when asked for the 'Type' and also when asked for the 'sides'
clc % Clears the command window
clear all % Clears the values that are assigned to the variable
close all % Closes all the graphs
Type = input("Give 1 for Hypotenuse or 2 for Vertical : "); % Asking user for Hypotenuse or Vertical
if Type == 1 % User wants to find hypotenuse
sides = input("Give [side1 side2] = "); % Asking user to enter the vertical sides' values
% Assiging easier symbols for the sides
a = sides(1,1);
b = sides(1,2);
if any(imag(sides)) ~= true && a >= 0 && b >= 0
figure(1)
% x- and y-coordinates of the triangle that will be graphed
x = [0 a 0 0];
y = [0 0 b 0];
plot(x,y)
title('Pythagorean theorem')
xlabel('Side 1')
ylabel('Side 2')
grid on
end
if any(imag(sides)) == true || a < 0 || b < 0
disp('No negative neither complex values for lengths are allowed. Please try again.') % Displays to the user that the lengths cannot be complex neither negative numbers
elseif a > 0 && b > 0
disp('The hypotenuse is :')
Hyp = sqrt(a^2 + b^2) % Calculation of the hypotenuse if both sides are given positive
elseif a == 0 && b == 0
Hyp = sqrt(a^2 + b^2)
disp('The triangle has become a single point') % Displays what happens when both vertical sides are zero
plot(x,y,'*')
grid on
else
Hyp = sqrt(a^2 + b^2)
disp('The triangle has become a single straight line') %Displays what happens when only one vertical side is zero
end
elseif Type == 2 %User wants to find a vertical side
sides = input("Give [Hyp side] = ");
% Assiging easier symbols for the hypotenuse and the sides
c = sides(1,1);
b = sides(1,2);
if any(imag(sides)) ~= true && c > 0 && b > 0 && c >= b
figure(1)
% x- and y-coordinates of the triangle that will be graphed
x = [0 sqrt(c^2 - b^2) 0 0];
y = [0 0 b 0];
plot(x,y)
xlabel('Side 1')
ylabel('Side 2')
grid on
end
if any(imag(sides)) == true || c < 0 || b < 0
disp('No negative neither complex values for lengths are allowed. Please try again') %Displays that the sides cannot be negative neither complex numbers
elseif c == 0 && b == 0
Vert = sqrt(c^2 - b^2)
x = [0 Vert 0 0];
y = [0 0 b 0];
disp('The triangle has become a single point') %Displays what happens when both Hyp and vertical are zero
plot(x,y,'*')
grid on
elseif c > 0 && b > 0 && c > b
Vert = sqrt(c^2 - b^2) %Calculation of the vertical side when the sides are both positive and the hypotenuse is greater than the vertical
elseif c > 0 && b == 0 || c == b
Vert = sqrt(c^2 - b^2)
disp('The triangle has become a single straight line') % Displays what happens if the hypotenuse is positive but the side is equal to zero, or if the hypotenuse is equal to the side.
else
disp('Hyp must be greater than the side. Please try again.') %Displays what happens when Hyp is less than the vertical side
end
else
disp('Wrong Input. Please try again and type 1 if you want to calculate the hypotenuse or 2 for the vertical.') % Displays what happens when the user enters a number different than 1 or 2
end

采纳的回答

Voss
Voss 2021-12-21
Use the 's' option in your call to input(), which takes the input as a character array and does not interpret or evaluate it. Then you can check that it conforms to whatever you need, e.g., if it needs to be a scalar number use str2double on the input and check that it's ~isnan. So, in your code you might do something like this:
Type = input("Give 1 for Hypotenuse or 2 for Vertical : ", 's'); % Asking user for Hypotenuse or Vertical
Type = str2double(Type);
if Type == 1 % User wants to find hypotenuse
sides = input("Give [side1 side2] = ", 's'); % Asking user to enter the vertical sides' values
sides = str2num(sides); % use str2num for allowing non-scalars
if numel(sides) == 2 % make sure they entered two sides
sides = sides(:).'; % make sides a row vector
% Assiging easier symbols for the sides
a = sides(1,1);
b = sides(1,2);
% the rest of your code here
else
disp('Please try again and enter 2 sides.');
end
elseif Type == 2
sides = input("Give [Hyp side] = ", 's');
sides = str2num(sides); % use str2num for allowing non-scalars
if numel(sides) == 2 % make sure they entered two numbers
sides = sides(:).'; % make sides a row vector
% Assiging easier symbols for the hypotenuse and the sides
c = sides(1,1);
b = sides(1,2);
% the rest of your code here
else
disp('Please try again and enter a side and the hypotenuse.');
end
else
disp('Wrong Input. Please try again and type 1 if you want to calculate the hypotenuse or 2 for the vertical.') % Displays what happens when the user enters a number different than 1 or 2
end
  5 个评论
Left Terry
Left Terry 2021-12-28
Hello. Could you please explain in more detail what the lines you added do exactly? Thank you in advance.
Voss
Voss 2021-12-28
str2num('1')
ans = 1
str2double('1')
ans = 1
str2num('1 2')
ans = 1×2
1 2
str2double('1 2')
ans = NaN
str2num('1x')
ans = []
str2double('1x')
ans = NaN

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by