Azzera filtri
Azzera filtri

how to assign zero to a vaiable

1 visualizzazione (ultimi 30 giorni)
Imaad Aslam
Imaad Aslam il 28 Gen 2023
Risposto: Sarthak il 9 Feb 2023
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 Commenti
DGM
DGM il 28 Gen 2023
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 il 28 Gen 2023
questdlg to avoid coding the choices as numeric at all.

Accedi per commentare.

Risposte (1)

Sarthak
Sarthak il 9 Feb 2023
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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by