Conditional if loop for addition of three consecutive numbers in app designer interface

3 ビュー (過去 30 日間)
Lewis HC
Lewis HC 2023 年 5 月 28 日
コメント済み: Image Analyst 2023 年 5 月 30 日
Greetings to all, I am trying to solve this code in app designer: If the sum of three consecutive numbers is >75 I should get the word 'OK' otherwise 'NO', this is the interface with the names and the respective code: thank you!
x1=app.EditField.Value
x2=app.EditField_2.Value
x3=app.EditField_3.Value
x4=app.EditField_4.Value
if x1+x2+x3>75
app.EditField_5.Value=disp('OK');
elseif x2+x3+x4>75
app.EditField_5.Value=disp('OK');
else app.EditField_5.Value=disp('NO');
end

回答 (2 件)

VBBV
VBBV 2023 年 5 月 28 日
編集済み: VBBV 2023 年 5 月 28 日
if (x1+x2+x3)>75
app.EditField_5.Value=disp('OK');
elseif (x2+x3+x4)>75
You need to use parenthesis ( ) in the if-else condition as shown
  1 件のコメント
Image Analyst
Image Analyst 2023 年 5 月 29 日
Not needed. Addition takes place before >:
  1. Parentheses ()
  2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
  3. Power with unary minus (.^-), unary plus (.^+), or logical negation (.^~) as well as matrix power with unary minus (^-), unary plus (^+), or logical negation (^~).NoteAlthough most operators work from left to right, the operators (^-), (.^-), (^+), (.^+), (^~), and (.^~) work from second from the right to left. It is recommended that you use parentheses to explicitly specify the intended precedence of statements containing these operator combinations.
  4. Unary plus (+), unary minus (-), logical negation (~)
  5. Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)
  6. Addition (+), subtraction (-)
  7. Colon operator (:)
  8. Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)
  9. Element-wise AND (&)
  10. Element-wise OR (|)
  11. Short-circuit AND (&&)
  12. Short-circuit OR (||)

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


Image Analyst
Image Analyst 2023 年 5 月 29 日
編集済み: Image Analyst 2023 年 5 月 29 日
Try it this way (untested and not using disp):
x1 = app.EditField.Value
x2 = app.EditField_2.Value
x3 = app.EditField_3.Value
x4 = app.EditField_4.Value
if x1 + x2 + x3 > 75
app.EditField_5.Value ='OK';
elseif x2 + x3 + x4 > 75
app.EditField_5.Value = 'OK';
else
app.EditField_5.Value = 'NO';
end
Or you can combine the first two:
x1 = app.EditField.Value
x2 = app.EditField_2.Value
x3 = app.EditField_3.Value
x4 = app.EditField_4.Value
if (x1 + x2 + x3 > 75) || (x2 + x3 + x4 > 75)
app.EditField_5.Value = 'OK';
else
app.EditField_5.Value = 'NO';
end
Attach your .mlapp file if you need more help.
  2 件のコメント
Lewis HC
Lewis HC 2023 年 5 月 29 日
Thank you for your help dear friends!
Image Analyst
Image Analyst 2023 年 5 月 30 日
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by