Error:" Output argument "y" (and maybe others) not assigned during call to"

조회 수: 1 (최근 30일)
B.E.
B.E. 2020년 1월 14일
편집: Image Analyst 2020년 1월 15일
I need help, I have this error " Output argument "y" (and maybe others) not assigned during call to"
function y=f(t)
d=2;
dstep=0.5;
T1=606;
if (t< T1) | (t>=T1+42)
y=0;
else
for i=0:5
if (T1+i*7<=t) & (t<=T1+i*7+dstep)
y=d;
elseif (T1+i*7+dstep<t) & (t<T1+i*7+1)
y=0;
elseif (T1+i*7+1<=t) & (t<=T1+i*7+1+dstep)
y=d;
elseif (T1+i*7+1+dstep<t) & (t<T1+i*7+2)
y=0;
elseif (T1+i*7+2<=t) & (t<=T1+i*7+2+dstep)
y=d;
elseif (T1+i*7+2+dstep<t) & (t<T1+i*7+3)
y=0;
elseif (T1+i*7+3<=t) & (t<=T1+i*7+3+dstep)
y=d;
elseif (T1+i*7+3+dstep<t) & (t<T1+i*7+4)
y=0;
elseif (T1+i*7+4<=t) & (t<=T1+i*7+4+dstep)
y=d;
elseif (T1+i*7+4+dstep<t) & (t<T1+i*7+7)
y=0;
end
end
end
  댓글 수: 8
B.E.
B.E. 2020년 1월 15일
편집: per isakson 2020년 1월 15일
I use the version
per isakson
per isakson 2020년 1월 15일
Version 7.6 (R208a) supports nonscalar expressions in if-elseif-else according to the documentation.

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

답변 (2개)

David Hill
David Hill 2020년 1월 15일
y might never get assigned in your function. What do you want y to be if it doesn't meet any of your conditional statements? Just assign y at the beginning (i.e., y=0;)
  댓글 수: 1
Image Analyst
Image Analyst 2020년 1월 15일
편집: Image Analyst 2020년 1월 15일
And change | to ||:
function y = f(t)
y = 0; % Initialize y in case we don't enter the if block.
d=2;
dstep=0.5;
T1=606;
if (t < T1) || (t >= T1 + 42)
y = 0;
else
for i = 0 : 5
if (T1+i*7<=t) & (t<=T1+i*7+dstep)
y=d;
elseif (T1+i*7+dstep<t) & (t<T1+i*7+1)
y=0;
elseif (T1+i*7+1<=t) & (t<=T1+i*7+1+dstep)
y=d;
elseif (T1+i*7+1+dstep<t) & (t<T1+i*7+2)
y=0;
elseif (T1+i*7+2<=t) & (t<=T1+i*7+2+dstep)
y=d;
elseif (T1+i*7+2+dstep<t) & (t<T1+i*7+3)
y=0;
elseif (T1+i*7+3<=t) & (t<=T1+i*7+3+dstep)
y=d;
elseif (T1+i*7+3+dstep<t) & (t<T1+i*7+4)
y=0;
elseif (T1+i*7+4<=t) & (t<=T1+i*7+4+dstep)
y=d;
elseif (T1+i*7+4+dstep<t) & (t<T1+i*7+7)
y=0;
end
end
end
But did you notice that y gets overwritten each time in your for loop? So y will end up with whatever y it decides to use for the final i=5 case. All the iterations from 0 to 4 will be ignored because the y for those iterations gets overwritten.

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


per isakson
per isakson 2020년 1월 15일
편집: per isakson 2020년 1월 15일
I found this on if-elseif-else in the documentation of R14.
Nonscalar Expressions
If the evaluated expression yields a nonscalar value, then every element of this value must be true or nonzero for the entire expression to be considered true. For example, the statement if (A < B) is true only if each element of matrix A is less than its corresponding element in matrix B.
Matlab may contain the old code even if is not documented. Backward compatibility for old-timers.
For every elseif expression the Code Analyzer reports
When both arguments are numeric scalars, consider replacing | with || for performance.
I take that as a hint that the old code is still there.
The the documentation of recent releases don't say anything about scalar or nonscalar expressions. It goes without saying that the values of the expressions are scalar. (As far as I can see.)
The call f([606:609]') reproduces the error you report.
Output argument "y" (and maybe others) not assigned during call to "f>f_".
Error in f (line 7)
y = f_([606:609]')
Your expressions, e.g. (T1+i*7+dstep<t), are noncalar when f() is called with a vector.
What Matlab release are you using?
How did you call f() ?

카테고리

Help CenterFile Exchange에서 Electrical Block Libraries에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by