필터 지우기
필터 지우기

Error using ~= Matrix dimensions must agree during using while function

조회 수: 2 (최근 30일)
jarvan
jarvan 2014년 11월 4일
댓글: Walter Roberson 2017년 10월 7일
I was testing the while loop for my ATM script.
password = 'jjteam'
password =
ucsdmate
balance = num1str(9.5405*100,'%10.2f%')
lastlogindate = datenum(2014,11,2,11,7,39)
lastlogindate =
7.3591e+05
datestr(lastlogindate)
rate = num2str(0.02*100,'%5.2f%%')
I save above code as atm.mat.
load ('atm.mat')
inpass = input('Enter your password : ','s');
while inpass ~= mypassword
disp('The passwoard is wrong,please try again.')
inpass = input('Enter your password : ','s');
end
fprintf('Your password is right.\n')
However, when I try to test my script, it said Error using ~= Matrix dimensions must agree. And Error in atm ,while inpass ~= mypassword. I am not sure which part I did wrong. Can anyone point it out? Thanks

채택된 답변

Guillaume
Guillaume 2014년 11월 4일
편집: Guillaume 2014년 11월 4일
Use strcmp to compare strings. == and ~= will only work if the strings are the same length.
while ~strcmp(inpass, mypassword)
...
  댓글 수: 3
Abdul Kazi
Abdul Kazi 2017년 10월 7일
Hi, but what if I wanted it to work despite the strings not being same length?
Walter Roberson
Walter Roberson 2017년 10월 7일
If you are using character vectors and you want to be able to use == to compare character vectors that might be different lengths, and you do not want to use strcmp, then you will need to create a new eq() method for the @char class and you will need to ensure that you version is earlier on the MATLAB path than the built-in version.
If you are using R2016b or later, you could use the string data type instead of character vectors, such as
string(inpass) ~= string(mypasswd)

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

추가 답변 (1개)

Julia
Julia 2014년 11월 4일
Hi,
I don't see where you use "mypassword". You declare only the string "password".

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by