MATLAB: Output argument not assigned during call

MATLABnot assigned

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".

Best Answer

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.