Clear Filters
Clear Filters

Undefined Function ,,,,, for input arguments of type 'char'

9 views (last 30 days)
Thank you in advanced. Just creating a function for a student number checker. 'Yes this is an assignment'. I would very much appreciate any help. If the error is easy to spot please just give me a hint ahha. This is my function:
%Question 3, Student Number Success Function
function [ LoginSuccessful ] = ValidSN( Studentnumber )
% ValidSN is a function that asks for the user's Student Number
% and confirms that their input is valid within Griffith College
% The function will return "Login Successful" if the first input
% start's with a 's' or 'S' and is 8 inputs long is size
% and only number after the 's', 'S'.
% Function[LoginSuccessful/UnSuccessful] = ValidSN(studentnumber)
% Step 1, Check the length.
StudentNumberLength = size(Studentnumber);
if StudentNumberLength~=8;
disp('Your student number length is incorrent, it must be 8 charaecters long');
LoginSuccessful = 0;
end;
% Step 2, Check the input starts with an 's' or 'S'.
S_or_s = Studentnumber(1);
if S_or_s~= 's' && S_or_s~= 'S'
disp('First input must start with an ''s'' or an ''S''');
LoginSuccessful = 0;
end
% Step 3, Check input 2-to-8 are numerical inputs only.
for SNLength = 2:8
if Studentnumber(SNLength)~= 2:8
disp('Not a numerical input.')
LoginSuccessful = 0
else
disp('Login Successful');
url = 'https://www.mathsworks.com'
web = 'www.mathsworks.com'
LoginSuccessful = 1;
end
end
And I'm calling it from this script.
%%QUestion 3
% SNumber checker
% This script calls on the function "isValidStudentNumber"
clc
clear all
% Student Number Checker called to the function ValidSN
% Step 1, Requires Input
while 1
s = input('Student Number: ', 's');
[Login] = ValidSN(s);
disp(Login)
url 'https://www.mathsworks.com'
web = 'www.mathsworks.com'
end
  1 Comment
Jan
Jan on 15 Jan 2017
I've edited the code using the "{} Code" button to make it readable. Please do this by your own in the future - thanks.

Sign in to comment.

Accepted Answer

Niels
Niels on 15 Jan 2017
Edited: Niels on 15 Jan 2017
i run your code, and i did not get any error message
s = input('Student Number: ', 's');
[Login] = ValidSN(s);
disp(Login)
Student Number: s12873654
Your student number length is incorrent, it must be 8 charaecters long
Not a numerical input.
Not a numerical input.
Not a numerical input.
Not a numerical input.
Not a numerical input.
Not a numerical input.
Not a numerical input.
0
your problem is that your "check" if the input is a number is wrong
if Studentnumber(SNLength)~= 2:8 ???
first of all studentNumber is a string so Studentnumber(2) and so on are all string. So if you want to check if 2:8 are numerical inputs use str2num and check if its true
if isempty(str2num(Studentnumber(SNLength))) % is empty if argument is no number
  2 Comments
Walter Roberson
Walter Roberson on 15 Jan 2017
To test for '0' to '9' you can use isdigit(). To test against a restricted set of digits such as '2' to '8' you could either do a pair of tests,
if var>= '2' && var <= '8'
Or you can use
ismember(var, '2':'8')

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!