MATLAB Answers


problem with polyfit function

Asked by mounim on 14 Dec 2012

hey everyone, i ahope someone can help me with this code, what i am trying to do is plotting a fitting line. my graf is shown, but the fit line, is not where it should be, it is the wrong direction, and the wrong angle, please what i am doing wrong ?

this is my code :

for k=50:100
     oneRow = binaryImage(k, :);
     leftEdge = find(oneRow, 1, 'first');
     rightEdge = find(oneRow, 1, 'last');
     hold on;
     midpoints(k) = (leftEdge + rightEdge)/2;
     %plot the left and right edge
     plot(leftEdge, k, 'rx','LineWidth',2);
     plot(rightEdge, k, 'rx','LineWidth',2);
     plot(midpoints(k), k, 'yx','LineWidth',2);
new_x = linspace(1, 100);
coeffs = polyfit(X, midpoints, 1);
new_y = polyval(coeffs, new_x);
plot(new_x,new_y, '-','LineWidth',2);

any help will be appreciated thanks.


Log in to comment.


1 Answer

Answer by mounim on 14 Dec 2012
 Accepted Answer

It is okay, i figured that out !! ... i had to remove the zeros - from the array in my for-loop. so now it is working.


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!