Exclusion of inappropriate inputs in a function

In the function below the user must enter only numbers. How can I show a screen message if the user enters letters or other unwanted symbols in order to warn him that he entered an inappropriate input?
function [offsets,Nodes,lengths,angles] = grid_syr(x_i,y_i,nex,ney,L1,L2,theta,phi)
if L1 > 0 && L2 > 0 && nex > 0 && ney > 0 && mod(nex,1) == 0 && mod(ney,1) == 0 % Lengths, L1 and L2, must be positive numbers and the number of elements, nex and ney, must be positive and integers.
% Converting degrees to radians
theta = theta*pi/180;
phi = -phi*pi/180;
L2 = L2*sin(theta); % Vertical projection of side L2
x_e = L2*cos(theta); % Extra horizontal length
Nodes = zeros(nex*ney,4); % Initialize elements nodes' matrix
Coordinates = zeros((nex+1)*(ney+1),2); % Initialize nodes coordinates' matrix
% Creating the nodes' matrix
for j = 1:ney
for i = 1:nex
Nodes(i+(j-1)*nex,:) = [(i+(j-1)*(nex+1)) (i+1+(j-1)*(nex+1)) (i+1+j*(nex+1)) (i+j*(nex+1))];
end
end
rot = [cos(phi) -sin(phi);sin(phi) cos(phi)]; % Rotation matrix
% Creating the nodes coordinates' matrix
for j=1:ney+1
for i=1:nex+1
Coordinates((i+(j-1)*(nex+1)),:) = [(x_i+(j-1)*x_e+(i-1)*L1) (y_i+(j-1)*L2)]*rot;
end
end
% Displaying the abovementioned matrices
Nodes
Coordinates
for i=1:size(Nodes,1)
nexss = [(Coordinates(Nodes(i,1),1)) (Coordinates(Nodes(i,2),1)) (Coordinates(Nodes(i,3),1)) (Coordinates(Nodes(i,4),1)) (Coordinates(Nodes(i,1),1))];
neyss = [(Coordinates(Nodes(i,1),2)) (Coordinates(Nodes(i,2),2)) (Coordinates(Nodes(i,3),2)) (Coordinates(Nodes(i,4),2)) (Coordinates(Nodes(i,1),2))];
line(nexss,neyss,'Marker','.','Color','Green')
end
title('Grid')
xlabel('x')
ylabel('y')
for i=1:(nex+1)*(ney+1)
text(Coordinates(i,1),Coordinates(i,2)+L1/20,num2str(i),'FontSize',8); % Displaying the nodes' numbers in the graph
end
for i=1:(nex+1)*(ney+1)
text(Coordinates(i,1), Coordinates(i,2)-L2/40,['(' num2str(Coordinates(i,1)) ',' num2str(Coordinates(i,2)) ')'],'FontSize',8); % Displaying the nodes' coordinates in the graph
end
grid on
end
end

 Accepted Answer

function [offsets,Nodes,lengths,angles] = grid_syr(x_i,y_i,nex,ney,L1,L2,theta,phi)
if ~isnumeric(x_i) || ~isnumeric(y_i) ...
|| ~isnumeric(nex) || ~isnumeric(ney) ...
|| ~isnumeric(L1) || ~isnumeric(L2) ...
|| ~isnumeric(theta) || ~isnumeric(phi)
uiwait(errordlg('Non-numeric input!','Error','modal'));
offsets = [];
Nodes = [];
lengths = [];
angles = [];
return
end
% the rest of your function
end

4 Comments

Hello. It doesn't work. I called grid_syr(asdf,0,3,2,3,2,sixty,0) and it returned "Undefined function or variable 'asdf'." as an error. I don't want this.
Note that in your function call, you're not passing the literal text 'asdf'. You're attemptting to pass a variable named asdf. No such variable exists.
Ok, noted! If I call grid_syr('asdf',0,3,2,3,2,60,0) the function works, but I would like to exclude every case of inappropriate input. I tried ischar() and isstring() but these are not working.
The above does exclude those. It will exclude everything except numeric inputs.

Sign in to comment.

More Answers (0)

Categories

Find more on Geoscience in Help Center and File Exchange

Asked:

on 21 Feb 2022

Commented:

DGM
on 22 Feb 2022

Community Treasure Hunt

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

Start Hunting!