Error:" Output argument "y" (and maybe others) not assigned during call to"
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
per isakson
2020-1-15
Version 7.6 (R208a) supports nonscalar expressions in if-elseif-else according to the documentation.
回答(2 个)
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
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
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() ?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Electrical Block Libraries 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!