What is the error in my function? Error: Variable 'T' is not fully defined on some execution paths.

This is the code which I wrote for generating a Trapezoid curve for BLDC motor. It is showing the following error: 'Variable 'T' is not fully defined on some execution paths'. The code:
% code
function y = fcn(u)
%#codegen
Q = mod(u,2*pi);
if Q >=0 && Q<2*pi/3
T = 1;
elseif Q>= 2*pi/3 && Q< pi
T = 1-6*(Q-2*pi/3)/pi;
elseif Q>= pi && Q<5*pi/3
T = -1;
elseif Q>= 5*pi/3 && Q<2*pi
T = -1 + 6*(Q-5*pi/3)/pi;
end
y=T;

 採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 25 日
編集済み: Stephen23 2017 年 5 月 25 日
None of the branches is encountered if u is infinite or nan.
function y = fcn(u)
%#codegen
Q = mod(u,2*pi);
if Q<2*pi/3
T = 1;
elseif Q < pi
T = 1-6*(Q-2*pi/3)/pi;
elseif Q < 5*pi/3
T = -1;
elseif Q < 2*pi
T = -1 + 6*(Q-5*pi/3)/pi;
else
T = nan; %if data was inf or nan
end
y=T;

7 件のコメント

Stephen23
Stephen23 2017 年 5 月 25 日
@Walter Roberson: I changed the last elseif to else, that seemed to be the intention.
I am not convinced that is an appropriate output if the input is nan or inf.
Stephen23
Stephen23 2017 年 5 月 25 日
??? I apologize if that was not the intention. Surely an else will give an output, which resolves the original question.
Meghdeep Jana
Meghdeep Jana 2017 年 5 月 25 日
Thank you for the solution. It is working fine now.
Sorry, Stephen, I mis-read what you were saying.
I want to run this code
function D=inc(V,I)
Dinit=.574;
deltaD=0.001;
persistent Va Da Ia;
if isempty(Da)
Va=42.64;
Ia=2;
Da=Dinit;
end
dV=V-Va;
dI=I-Ia;
if dV== 0
if dI==0
else
if dI>0
D=Da-deltaD;
else
D=Da+deltaD;
end
end
else
if dI/dV==-I/V
else
if dI/dV>-I/V
D=Da-deltaD;
else
D=Da+deltaD;
end
end
end
Da=D;
Va=V;
Ia=I;
but Matlab shows this error
Variable 'D' is not fully defined on some execution paths.
小东 刘
小东 刘 2022 年 10 月 13 日
hello! I have the same problem now.may I ask how you solved it later ?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimscape Electrical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by