simple matlab function (simulink) with if?

3 visualizaciones (últimos 30 días)
SimTec
SimTec el 12 de Nov. de 2019
Respondida: Pramil el 29 de Nov. de 2024 a las 11:56
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
matlabfunction.PNG

Respuestas (1)

Pramil
Pramil el 29 de Nov. de 2024 a las 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.

Categorías

Más información sobre Verification, Validation, and Test en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by