while loop not working

2 views (last 30 days)
Harrison Forlines
Harrison Forlines on 28 Nov 2021
Answered: DGM on 28 Nov 2021
I'm trying to get my while loop to ask specific questions in the if statement based on what the input from the use if but i cant seem to format the while conditions correctly. For example, if the user left all of the 4 catagories blank, I would want the if statement that cooresponds to 4 catagories blank to execute and ask the 4 questions again. Another example would be if the user only entered one value for either Pact, Pre, or Papp, the only value we care about is the power factor PF, which can be calculated from the 3 other values, thus if the only value was for example Pact, I woukd want the if statment cooresponding to having left blank PF, Pre, and Papp to execute and reask the questions cororesponding to PF, Pre, & Papp. Thank you in advance for any help. :)
Pact = input('Input value of active/real power (Pact), or press enter if N/A: ');
Pre = input('Input value of reactive power (Pre), or press enter if N/A: ');
Papp = input('Input value of apparent power (Papp), or press enter if N/A: ');
PF = input('Input value of power factor (PF), or press enter if N/A: ');
while (isempty(PF) && isempty(Pact) && isempty(Pre) && isempty(Papp)) || (isempty(Pact) && isempty(Pre)) || (isempty(Pact) && isempty(Papp)) || (isempty(Pre) && isempty(Papp))
if and(PF == isempty(PF) & Pact == isempty(Pact) & Pre == isempty(Pre) & Papp == isempty(Papp))
disp('Error, must input valid combination of Pact, Pre, Papp, & PF')
break
else
PF = input('Input value of power factor (PF), or press enter if N/A: ');
Pact = input('Input value of active/real power (Pact), or press enter if N/A: ');
Pre = input('Input value of reactive power (Pre), or press enter if N/A: ');
Papp = input('Input value of apparent power (Papp), or press enter if N/A: ');
end
if isempty(PF) && ~isempty(Pact) && isempty(Pre) && isempty(Papp)
disp('Error, must input valid combination of Pact, Pre, Papp, & PF')
break
else
PF = input('Input value of power factor (PF), or press enter if N/A: ');
Pre = input('Input value of reactive power (Pre), or press enter if N/A: ');
Papp = input('Input value of apparent power (Papp), or press enter if N/A: ');
end
if isempty(PF) && isempty(Pact) && ~isempty(Pre) && isempty(Papp)
disp('Error, must input valid combination of Pact, Pre, Papp, & PF')
break
else
PF = input('Input value of power factor (PF), or press enter if N/A: ');
Pact = input('Input value of active/real power (Pact), or press enter if N/A: ');
Papp = input('Input value of apparent power (Papp), or press enter if N/A: ');
end
if isempty(PF) && isempty(Pact) && isempty(Pre) && ~isempty(Papp)
disp('Error, must input valid combination of Pact, Pre, Papp, & PF')
break
else
PF = input('Input value of power factor (PF), or press enter if N/A: ');
Pact = input('Input value of active/real power (Pact), or press enter if N/A: ');
Pre = input('Input value of reactive power (Pre), or press enter if N/A: ');
end
if ~isempty(PF)
break
end
end

Answers (1)

DGM
DGM on 28 Nov 2021
This isn't exactly an elegant solution, but if we're gathering a bunch of parameters using input(), I think we've already crossed that bridge.
A minimum of any two parameters are required.
firstpass = true;
emptyinputs = [1 1 1 1];
while nnz(~emptyinputs)<2
if firstpass
firstpass = false;
else
disp('Error, must input valid combination of Pact, Pre, Papp, & PF')
end
for q = find(emptyinputs)
switch q
case 1
PF = input('Input value of power factor (PF), or press enter if N/A: ');
case 2
Pact = input('Input value of active/real power (Pact), or press enter if N/A: ');
case 3
Pre = input('Input value of reactive power (Pre), or press enter if N/A: ');
case 4
Papp = input('Input value of apparent power (Papp), or press enter if N/A: ');
end
end
emptyinputs = [isempty(PF) isempty(Pact) isempty(Pre) isempty(Papp)];
end

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!