simple matlab function (simulink) with if?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have written the very basic MAtlab function on matlab and would like why I am getting this error:
function [pos, neg] = select_output(err)
% if the error is negative , flow through the PID designed for negativ
% errors otehrwise positiv
if err >= 0
pos=err;
else
neg=err;
end
0 commentaires
Réponses (1)
Pramil
le 29 Nov 2024 à 11:56
Hello SimTec,
The error message you're encountering indicates that the output variable "pos" is not be assigned a value on some execution paths.
In MATLAB, all output variables need to be assigned a value in every possible execution path of the function. In your current implementation, if "err" is negative, "pos" is not assigned any value, which causes the error.
To fix this, you should initialize both "pos" and "neg" to default values at the beginning of your function. This ensures that they are always assigned, regardless of the condition. Here's how you can modify your function:
function [pos, neg] = select_output(err)
% Initialize outputs
pos = 0;
neg = 0;
% If the error is negative, flow through the PID designed for negative
% errors otherwise positive
if err >= 0
pos = err;
else
neg = err;
end
end
Hope it helps.
0 commentaires
Voir également
Catégories
En savoir plus sur Verification, Validation, and Test dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!