필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
Lewis Samuels
Lewis Samuels 2017년 1월 15일
댓글: Lewis Samuels 2017년 1월 30일
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
Jan
Jan 2017년 1월 15일
I've edited the code using the "{} Code" button to make it readable. Please do this by your own in the future - thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Niels
Niels 2017년 1월 15일
편집: Niels 2017년 1월 15일
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
Walter Roberson
Walter Roberson 2017년 1월 15일
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')
Lewis Samuels
Lewis Samuels 2017년 1월 30일
Thank you. I used this in another code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by