Variable ... is undefined on some execution paths

조회 수: 9 (최근 30일)
Federico Avino
Federico Avino 2020년 11월 30일
답변: Walter Roberson 2020년 11월 30일
I'm doing a project in Simulink/Simscape, and it contains a Matlab Function block, which contains the code that must be executed each cycle by the simulator. In this block i need to calculate the discrete derivative of temperature doing (T1-T1_prev)/dt, where T1 is one of the inputs of the block.
In theory, the problem of this methode is the definition of T1_prev at the time 0 of the simulation, so i did an
function y = TCU(u)
%% input
...
time = u(6);
if time <= 0
...
T1_prev = 0;
end
temperature_tank1 = temperatureCheck (T1, T1_prev, Tmax, Tsig, dT_dt_max, deltat);
T1_prev = T1;
y
end
I reported just the parts which i have problems with. In this way matlab says T1_prev is not fully defined due to the lack of an else case, but actually i don't want it, and T1_prev results fully defined, because it's updated in the end of the cycle for each cycle that is not the first. (T1 is updated each cycle too)
How can i solve the problem?
Thank you guys
  댓글 수: 2
John D'Errico
John D'Errico 2020년 11월 30일
Learn to use the debugger? Clearly, even though you think it is always defined, there are some paths where it is not.
Federico Avino
Federico Avino 2020년 11월 30일
Thank you, I know technically it's not fully defined for the compiler (absence of T1_prev in else case), but for the simulation it is defined because the structure of the execution, so the real case, is cyclical. How can I manage to overcome this false error?

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 30일
persistent T1_prev
if isempty(T1_prev); T1_prev = 0; end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Physical Units에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by