simple matlab function (simulink) with if?

1 次查看(过去 30 天)
SimTec
SimTec 2019-11-12
回答: Pramil 2024-11-29
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

回答(1 个)

Pramil
Pramil 2024-11-29
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.

类别

Help CenterFile Exchange 中查找有关 Verification, Validation, and Test 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by