How to detect negative number in Switch and case statement
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Telema Harry
el 10 de Ag. de 2021
Comentada: Telema Harry
el 10 de Ag. de 2021
I want to implement some control actions based on the sign of the error and Uz.
when error = -1, Uz = -1.
The output is 'other value' instead of 'Object losing altitude'.
Please how can I correct it.
Thank you for the help.
error = -1;
Uz = -1;
switch error
case error > 0 && Uz > 0
disp('Controller should not take any action')
case error > 0 && Uz < 0
disp('Open the control valve and supply more lifting gas')
case error < 0 && Uz < 0
disp('Object losing altitude')
otherwise
disp('other value')
end
0 comentarios
Respuesta aceptada
Scott MacKenzie
el 10 de Ag. de 2021
Look carefully. Because you are switching on error, your first case expression reduces to
error == (error < 0 && Uz > 0)
which is false. In fact, all your case expressions are false so the otherwise statement always executes.
The fix: Just use an if-else arrangement:
error = -1;
Uz = -1;
if error > 0 && Uz > 0
disp('Controller should not take any action')
elseif error > 0 && Uz < 0
disp('Open the control valve and supply more lifting gas')
elseif error < 0 && Uz < 0
disp('Object losing altitude')
else
disp('other value')
end
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!