simple matlab function (simulink) with if?

조회 수: 3 (최근 30일)
SimTec
SimTec 2019년 11월 12일
답변: Pramil 2024년 11월 29일 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

답변 (1개)

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

카테고리

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