MATLAB Answers

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

3 views (last 30 days)
B.E.
B.E. on 14 Jan 2020
Edited: Image Analyst on 15 Jan 2020
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 Comments

Show 5 older comments
per isakson
per isakson on 15 Jan 2020
Regarding the first problem, did you read and understand my answer?
per isakson
per isakson on 15 Jan 2020
Version 7.6 (R208a) supports nonscalar expressions in if-elseif-else according to the documentation.

Sign in to comment.

Answers (2)

David Hill
David Hill on 15 Jan 2020
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 Comment

Image Analyst
Image Analyst on 15 Jan 2020
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.

Sign in to comment.


per isakson
per isakson on 15 Jan 2020
Edited: per isakson on 15 Jan 2020
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 Comments

Sign in to comment.

Sign in to answer this question.


Translated by