Variable Not fully defined on some execution path error

조회 수: 98 (최근 30일)
John Petersen
John Petersen 2021년 3월 11일
댓글: John Petersen 2021년 3월 11일
I have some code that is inside a simulink block that gets an error when it's compiled. It says a variable is not fully defined on some execution paths, but it's not true.
Here's a simplified version of the code:
function myfunc(logicA, logicB)
A = logicA;
B = logicB; % also input from the function
if A
X = 1;
elseif B
X = 2;
end
if (A || B)
Y = X;
end
It complies fine if I change it to
function myfunc(logicA, logicB)
A = logicA;
B = logicB; % also input from the function
X=2
if A
X = 1;
end
if (A || B)
Y = X;
end
It looks like the compiler doesn't realize that it doesn't need X if (A || B) is false?

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 11일
disp(myfunc(1,1))
1
disp(myfunc(1,0))
1
disp(myfunc(0,1))
2
disp(myfunc(0,0))
Output argument "Y" (and maybe others) not assigned during call to "solution>myfunc".
function Y = myfunc(logicA, logicB)
A = logicA;
B = logicB; % also input from the function
if A
X = 1;
elseif B
X = 2;
end
if (A || B)
Y = X;
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 3월 11일
The code would only be acceptable if you do not unconditionally use Y afterwards.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by