필터 지우기
필터 지우기

while loop in matlab password GUI

조회 수: 13 (최근 30일)
Pat
Pat 2013년 5월 7일
Hi, i want to create a matlab program that will require the user to enter the username and password before they can gain access to the program. so far i have able to do it if the correct password is enter but i cannot get the program to loop corretly. i want if the user enter the wrong combination of password/username lets say maximum 3 times, the system will terminated. plz help me guys...waste a lot of time on doing this.
username1=*****;
password1=****;
username = upper(get(handles.edit1,'string'));
password = upper(get(handles.edit6,'string'));
if(strcmpi(username,username1))
if (strcmpi(password,password1))
disp('successfully log into the system');
else
close all
end
else
close all
end

채택된 답변

David Sanchez
David Sanchez 2013년 5월 8일
Two things: first, you should create a variable (global) called attempts, this variable is initialize at the beginning, right after the GUI is opened. The rest of the code:
% this goes in your login pushbutton callback function
username = get(handles.usrbox,'string');
password = get(handles.psswrdbox, 'string');
attempts = attempts + 1;
if strcmpi(username, username1)&&strcmpi(password,password1)
disp('b')
elseif attempts == 3
disp('a');
end
has to be placed on the callback function. Do you insert a login pushbutton? ( A button that has to be pushed in order to login ) If not, the code should be pasted on the edit box callback function (watch out! I guess you have two edit boxes, one for username and another for password, paste it on the password edit box callback ). I would advice to insert a login pushbutton, but that's up to you.
  댓글 수: 1
Pat
Pat 2013년 5월 8일
hi this code works..thank you so much for helping me:-)

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

추가 답변 (5개)

David Sanchez
David Sanchez 2013년 5월 7일
I hope following code is of help
username1 = 'xxx';
password1 = 'yyy';
attemps = 0;
while attemps < 4
username = input('intoduce user name: ');
password = input('intoduce password: ');
if(strcmpi(username,username1))&&(strcmpi(password,password1))
disp('successfully log into the system');
return
else
attemps = attemps + 1;
end
end
  댓글 수: 1
Pat
Pat 2013년 5월 7일
편집: Pat 2013년 5월 7일
thank for your reply, i modified the code a bit to suit my gui. below shown the code that i made but when i try to enter false username and password, the count just go to 3 thus the a value is shown in command window. i just enter the username and password 1 time only. what is wrong with my code
username1= 'xxx';
password1= 'yyy';
attempt= 0;
while attempt < 3
username = upper(get(handles.edit1,'string'));
password = upper(get(handles.edit6,'string'));
if(strcmpi(username,username1))&&(strcmpi(password,password1))
disp('b');
return
else
attempt = attempt + 1;
end
end
disp('a');

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


David Sanchez
David Sanchez 2013년 5월 8일
Hi, since disp('a'); is outside the while loop, it will always be displayed. You can set a condition if to display 'a' only if 3 attempts were made
username1= 'xxx';
password1= 'yyy';
attempt= 0;
while attempt < 3
username = upper(get(handles.edit1,'string'));
password = upper(get(handles.edit6,'string'));
if(strcmpi(username,username1))&&(strcmpi(password,password1))
disp('b');
return
else
attempt = attempt + 1;
end
end
if attempt == 3
disp('a');
end
I hope it helped you, do not forget to vote if so.
  댓글 수: 1
Pat
Pat 2013년 5월 8일
편집: Pat 2013년 5월 8일
sir i have try the code that you suggested but unfortunately the problem still are there as matlab display the character 'a' although i had only test one fail attempt. i think there are problem in the loop itself as it seem does not wait for user to key in second and third fail attempt before exiting the loop.

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


David Sanchez
David Sanchez 2013년 5월 8일
Hi, that's a bit awkward, with the code above, disp('a') only comes to work when attempt equals 3 ( if attempt == 3 ). I tested it myself and 'a' is only displayed after three unsuccessful attempts were made. You can try the following version too: ( adapt it for you GUI later, it is easier to debug code outside the GUI, once it works as you want, insert it in the GUI )
username1= 'xxx';
password1= 'yyy';
attempt= 0;
while attempt < 3
username = input('intoduce user name: ');
password = input('intoduce password: ');
if(strcmpi(username,username1))&&(strcmpi(password,password1))
disp('b');
return
else
attempt = attempt + 1;
if attempt == 3
disp('a');
end
end
end
  댓글 수: 3
Teja Muppirala
Teja Muppirala 2013년 5월 8일
But first change this:
username = input('intoduce user name: ');
password = input('intoduce password: ');
To this:
username = input('intoduce user name: ','s');
password = input('intoduce password: ','s');
to indicate the input is a string, otherwise you'll have to put quotes in your input to distinguish it from a variable.
Pat
Pat 2013년 5월 8일
편집: Pat 2013년 5월 8일
mind to share anything that i need to do to adjust my gui to suit this code. i use two edit box, 1 for username input and the other one for the password input. i use this code for username/password input
username = upper(get(handles.edit1,'string'));
password = upper(get(handles.edit6,'string'));

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


Teja Muppirala
Teja Muppirala 2013년 5월 8일
In your GUI case, the reason it displays 'a' after just one failed attempt is, this loop:
username1= 'xxx';
password1= 'yyy';
attempt= 0;
while attempt < 3
username = upper(get(handles.edit1,'string'));
password = upper(get(handles.edit6,'string'));
if(strcmpi(username,username1))&&(strcmpi(password,password1))
disp('b');
return
else
attempt = attempt + 1;
end
end
if attempt == 3
disp('a');
end
runs 3 times instantly before you can even change the values in the edit boxes. I'm assuming this code is in some callback, like a pushbutton or something. You're going to need to check the password only once every time the user runs this callback function, and keep track of how many times they tried/failed. For example, you might store the variable attempt as 'Userdata' or use SETAPPDATA or something. Then increment it once everytime the user submits the password.

David Sanchez
David Sanchez 2013년 5월 8일
Teja got it right, I think next code would be helpful ( it needs to be adapted to your GUI )
attempts = 0; % do not initialize this variable in pushbutton callback function
% this goes in your login pushbutton callback function
username = get(handles.usrbox,'string');
password = get(handles.psswrdbox, 'string');
attempts = attempts + 1;
if strcmpi(username, username1)&&strcmpi(password,password1)
disp('b')
elseif attempts == 3
disp('a');
end
  댓글 수: 1
Pat
Pat 2013년 5월 8일
편집: Pat 2013년 5월 8일
sir where should i paste this code to? it is in the openingFcn or the edit box callback

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by