Output argument not assigned during call

조회 수: 1 (최근 30일)
Pavel M
Pavel M 2020년 2월 27일
답변: Image Analyst 2020년 2월 27일
I need help to correct this code. While I did not write this code in function, it worked right, but now matlab gives error
[X, Y] = points(t1, U1, U01)
function [X, Y] = points(t, U, U0)
d = U-U0;
s = abs(diff(sign(d)));
id = find( s>0 );
h = zeros(size(id));
f = zeros(size(id));
for k = 1:length(id)
i = id(k);
a1 = (U0(i+1)-U0(i))/(t(i+1)-t(i));
a2 = (U(i+1)-U(i))/(t(i+1)-t(i));
b1 = U0(i)-a1*t(i);
b2 = U(i)-a2*t(i);
A = [-a1, 1;
-a2, 1];
B = [b1; b2];
u = A\B;
X(k) = u(1);
Y(k) = u(2);
end
end
Output argument "X" (and maybe others) not assigned during call to "points".

채택된 답변

Fangjun Jiang
Fangjun Jiang 2020년 2월 27일
편집: Fangjun Jiang 2020년 2월 27일
X and Y are only assigned inside the for-loop, the for-loop is dependent on the value of "id", which is dependent on the result of find(). What if find() returns empty?
Always assign a default return value to avoid this problem.
  댓글 수: 2
Pavel M
Pavel M 2020년 2월 27일
Ok, id really returns empty, but while I did not write this code in function, it worked right. Why now id returns empty?
Fangjun Jiang
Fangjun Jiang 2020년 2월 27일
It depends on U0 and U. Try to run and understand these three lines
d = U-U0;
s = abs(diff(sign(d)));
id = find( s>0 )

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 2월 27일
The function should start like this:
function [X, Y] = points(t, U, U0)
X = []; % Make sure we return SOMETHING, even though it's empty, even if we never assign X.
Y = []; % Make sure we return SOMETHING, even though it's empty, even if we never assign Y.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by