Skipping if Statement even though a has been set to 0, on matlab GUI

2 ビュー (過去 30 日間)
Oliver Ferenczi
Oliver Ferenczi 2019 年 11 月 4 日
コメント済み: Adam Danz 2019 年 11 月 4 日
Hi, My code skips this if statement when I set a to 0, why does this happen? It should go into the if statement and display a but it doesn't.
a = get(handles.A_Value,'String');
if a == 0
disp(a)
end
Thank you!
  1 件のコメント
Bob Thompson
Bob Thompson 2019 年 11 月 4 日
When you try putting a debugger mark on this if statement, does it actually consider the if, or is something in the rest of your code causing it to skip over the statement entirely?
Could you share some more of the rest of your code? I would agree that it doesn't make sense that the if statement is never true, based on what you have shown, which makes me thing the problem isn't in the if statement itself, but somewhere in the rest of the code.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 4 日
編集済み: Adam Danz 2019 年 11 月 4 日
a = get(handles.A_Value,'String');
% a is a string, not a number.
% Option 1 is to convert a to a number
a = str2double(get(handles.A_Value,'String'));
if a == 0
disp(a)
end
% option 2 is to compare strings rather than numbers
a = get(handles.A_Value,'String');
if strcmp(a, '0')
disp(a)
end
% but be careful because what if user enters 0.0
% in that case '0' is different than '0.0'
  2 件のコメント
Oliver Ferenczi
Oliver Ferenczi 2019 年 11 月 4 日
Thank you!
Adam Danz
Adam Danz 2019 年 11 月 4 日
Glad I could help. Option 1 is the way to go (using str2double).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by