Output argument "XXX" (and maybe others) not assigned during call to "function".

1 次查看(过去 30 天)
Not sure what this means. Code here:
global T d t v
T = 10;
d = 0.3;
[t,v]= ode23(@pwm,[0:1e-7:0.1], [0 0], options);
v = pwm(t,T,d);
figure,plot(t,v); %%Plot the datas
%Below is the function declaration, in a different file in the same folder.
function v = pwm(t,T,d)
if T <= t <d*T
v = 1;
end
if d*T <= t <T
v = 0;
end
end

回答(1 个)

Star Strider
Star Strider 2020-1-20
First — Please do not use global variables! They create more problems than they aolve, and can make code very difficult to debug. Instead pass them as extra parameters as decribed in Passing Extra Parameters.
Second — If neither of the if conditions are satisfied, ‘v’ may not be assigned. One way to deal with that is to assign it as NaN in the beginning, then over-write that assignment if at least one of the if blocks are satisfied:
function v = pwm(t,T,d)
v = NaN;
if T <= t <d*T
v = 1;
end
if d*T <= t <T
v = 0;
end
end
  1 个评论
Steven Lord
Steven Lord 2020-1-20
Third -- the idiom "T <= t < d*T" doesn't do what you think it does. It is equivalent to "(T <= t) < d*T". Since (T <= t) can only take on the values false (0) or true (1), if d*T is greater than 1 the result of that expression will always be true.
Code Analyzer in the MATLAB Editor should have underlined that code in orange, suggesting that you break that up into two pieces:
if (T <= t) & (t < d*T)
Note that this is different from what your original version does:
if (T <= t) < d*T

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by