Problem with an If loop / Gui application

조회 수: 2 (최근 30일)
Manikandan
Manikandan 2011년 3월 9일
I have some problem in running the if loop. I get the string value of two text boxes using the get command. Next, I check if both of them has 'high' as their string. If yes, the msgbox appears (works). If no, the msgbox inside the else statement does not get executed. My knowledge in MATLAB is limited. Kindly help me out of this.
p1 = get(handles.n_text,'string')
p2 = get(handles.c_text,'string')
if (p1 == 'high')&(p2 == 'high')
msgbox('Difficult')
else
msgbox('easy')
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 8일
There is no such thing as an if loop.

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

채택된 답변

Manikandan
Manikandan 2011년 3월 9일
Thank you Walter and Jan. It works.
  댓글 수: 2
Jan
Jan 2011년 3월 9일
I've deleted my answer. I have not seen Walter's answer when I started to create mine. But Walter's was exhaustive already.
Oleg Komarov
Oleg Komarov 2011년 3월 10일
-1 to OP for accepting his own unmeaningful answer.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 3월 9일
Use strcmp() or strcmpi() instead of == . When you use == then if either of the strings are not exactly 4 characters long, the == comparison will fail with an error because == is an element-by-element comparison. p1(1) to be compared to 'h', p1(2) to be compared to 'i', and so on, which will fail if p1 does not have exactly 4 elements (characters).
When the routine fails with an error, the msgbox() in the else will not get executed.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by