필터 지우기
필터 지우기

I thought this one is interesting (Simulink)

조회 수: 1 (최근 30일)
bethel o
bethel o 2012년 8월 7일
The two functions below are basically the same but the second one includes an else statement at the end. They are functions for matlab function block in simulink. The matlab function block gives error on the first one(regarding unset output in some execution path) but not in the second one even though the else statement on the second function is never needed. Should the compiler no better of is this acceptable? The input signal is shown below:
input signal:
u=256*10;
Averaging=256;
function y = fcn(u,Averaging)
%#codegen
persistent dwork1;
%coder.extrinsic('disp');
assert(Averaging<=256+1);
if(isempty(dwork1))
dwork1=zeros(Averaging+1,2);
dwork1(1)=1;
else
dwork1(1)=dwork1(1)+1;
end
avg2=2*Averaging;
if(dwork1(1)<=Averaging)
dwork1(dwork1(1)+1,1)=u(1);
y=-2;
elseif(dwork1(1)>Averaging && dwork1(1)<=avg2)
dwork1(dwork1(1)+1-Averaging,2)=u(1);
y=-1;
elseif(dwork1(1)>avg2)
%CAUTION: the input u is ignored here
dwork1(1)=Averaging+1;
d1=dwork1(2:end,2)-dwork1(2:end,1);
y=length(d1(d1<0));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = fcn(u,Averaging)
%#codegen
persistent dwork1;
%coder.extrinsic('disp');
assert(Averaging<=256+1);
if(isempty(dwork1))
dwork1=zeros(Averaging+1,2);
dwork1(1)=1;
else
dwork1(1)=dwork1(1)+1;
end
avg2=2*Averaging;
if(dwork1(1)<=Averaging)
dwork1(dwork1(1)+1,1)=u(1);
y=-2;
elseif(dwork1(1)>Averaging && dwork1(1)<=avg2)
dwork1(dwork1(1)+1-Averaging,2)=u(1);
y=-1;
elseif(dwork1(1)>avg2)
%CAUTION: the input u is ignored here
dwork1(1)=Averaging+1;
d1=dwork1(2:end,2)-dwork1(2:end,1);
y=length(d1(d1<0));
else
%CAUTION: this portion should never be executed, therefore y should
%never be equal to -10
y=-10;
%disp(dwork1(1))
end

답변 (1개)

Walter Roberson
Walter Roberson 2012년 8월 8일
At the time of compilation, the routine does not know that Averaging cannot be NaN.
  댓글 수: 1
bethel o
bethel o 2012년 8월 8일
hmm but let me be pedantic, the computer should probably let the programmer control stuff. The programmer is probabily wise enough to know that 'Averaging' can never be NaN and that was why he wrote his code like that. But the computer who does not know what 'Averaging' is thinks that the programmer is just a stupid human and that 'Averaging' can be NaN(how rebellious).
Actually the programmer of the first function wanted the programme to crash when 'y' was not set. But it seems it is impossible to crash the computer in this way not because the computer is incapable but because the computer thinks he knows better.
And by the way a C code written in that way would happily compile(obedient computer).

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

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by