尽管loop not working

1视图(最近30天)
Harrison Forlines
Harrison Forlines 2021年11月28日
Answered: DGM 2021年11月28日
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(“表观功率的输入值(PAPP),或者在N/A时按Enter:');
PF = input('Input value of power factor (PF), or press enter if N/A: ');
尽管(isempty(PF) && isempty(Pact) && isempty(Pre) && isempty(Papp)) || (isempty(Pact) && isempty(Pre)) || (isempty(Pact) && isempty(Papp)) || (isempty(Pre) && isempty(Papp))
if和(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(“表观功率的输入值(PAPP),或者在N/A时按Enter:');
end
ifisempty (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(“表观功率的输入值(PAPP),或者在N/A时按Enter:');
end
ifisempty (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(“表观功率的输入值(PAPP),或者在N/A时按Enter:');
end
ifisempty (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 2021年11月28日
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];
尽管nnz(~emptyinputs)<2
iffirstpass
firstpass = false;
else
disp('Error, must input valid combination of Pact, Pre, Papp, & PF')
end
forq = find(emptyinputs)
转变q
case1
PF = input('Input value of power factor (PF), or press enter if N/A: ');
case2
Pact = input('Input value of active/real power (Pact), or press enter if N/A: ');
case3
Pre = input('Input value of reactive power (Pre), or press enter if N/A: ');
case4
Papp = input(“表观功率的输入值(PAPP),或者在N/A时按Enter:');
end
end
emptyInputs = [ISEMPTY(PF)ISEMPTY(PACT)ISEMPTY(PRE)ISEMPTY(PAPP)];
end

s manbetx 845


发布

R2021a

Community Treasure Hunt

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

开始狩猎!