필터 지우기
필터 지우기

how to assign zero to a vaiable

조회 수: 1 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by