Error:" Output argument "y" (and maybe others) not assigned during call to"
2 ビュー (過去 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!