Output argument "XXX" (and maybe others) not assigned during call to "function".
조회 수: 2 (최근 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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!