how to assign zero to a vaiable

3 ビュー (過去 30 日間)
Imaad Aslam
Imaad Aslam 2023 年 1 月 28 日
回答済み: Sarthak 2023 年 2 月 9 日
dead_load = input('Enter the value of Dead Load (in kN/m) = ');
include_self_wght = input('Does the Dead Load include Self Weight? (1)Yes (2)No = ' );
if include_self_wght == 1
self_wght = 0;
else
self_wght = 25*(b/1000)*(D/1000);
end
if isempty(dead_load)
dead_load = 6;
end
I want the self wght to be 0 if the include_self-wght = 1
but whenever I run the code it always evaluates the expression irrespective of what the user has inputted
please help me
Thanks
  2 件のコメント
DGM
DGM 2023 年 1 月 28 日
I don't know what you were giving it, but when the user inputs "1", then self_wght is set to 0 as requested.
Bear in mind that the logic presented to the user is:
  • "1" is yes
  • "2" is no
... when in reality, it's
  • "1" is yes
  • any other number is no
  • any input that begins with a non-numeric character will not be accepted
  • any number followed by a non-numeric character will result in an error
If you're going to write your code around subjecting the user to a tedious series of input() prompts, you might want to think about the ergonomics of the affair from the user's perspective.
Ask the user for the specific information needed. If you need a binary flag that changes the program behavior, simply ask a yes/no question and check the leading character instead of asking the user to codify their own response in an arbitrary numeric format.
Safeguard against all the inputs that you might get, especially considering that this sort of interactive workflow is a great way to collect typos. This generally winds up making every instance of input() into a loop that tests for validity and re-prompts if the input is invalid.
Walter Roberson
Walter Roberson 2023 年 1 月 28 日
questdlg to avoid coding the choices as numeric at all.

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

回答 (1 件)

Sarthak
Sarthak 2023 年 2 月 9 日
I think if you put the value of include_self_wght == 1, i.e., ‘Yes’, then self_wght will be assigned a value of 0 as requested.
If you put 2 as an input, then it will show an error because the variables b and D are not defined.

カテゴリ

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