[Answer edited to reflect updates]
In your code, you used
C1=centroids(1,1);
C2=centroids(1,2);
After this step, C1 and C2 become single-element scalars. You can check this with size(C1)
and size(C2)
, which will return [1 1]
as the answer. I'm guessing that you wanted to plot the first C1- and C2-point, then extend it all the way to element pair N. That's not necessary, the plot
function can handle vectors (and even matrices, but that will appear as a series of plots).
I'm not familiar with the image processing toolbox, and I do not have that toolbox so I can't check the function outputs. But as far as I can tell, what you need is a 30-row 2-column array of centroid position data, from which you will curve-fit its position at unknown x. I've removed some plotting functions; I trust this will make the code clearer.
for N=1:30
I = figure.image.(['j' num2str(N)]);
bw=(I);
ss = bwlabel(bw);
s = regionprops(bw,'centroid');
centroids = cat(1, centroids, s.Centroid); %Concatenate s.Centroid below centroids
end
%At this point, "centroids" should be a 30-by-2 array
size(centroids) % check if the output from this is [30 2]
x=centroids(:,1);
y=centroids(:,2);
poly=polyfit(x,y,2); %poly is a vector of curve-fitted polynomial coefficients
pval=polyval(poly,x); %pval is a vector of curve-fitted values evaluated at x
parabola=plot(x,pval); %plot the parabola
To get the y-position of the parabola at a point x_i, use polyval(poly,x_i)
.
Note particularly the proper syntax for the cat
function; cat(A,B) concatenates B below A, you can't use cat
with only one argument, as in your original code. This is likely the root cause of your headache, since with only 1 argument MATLAB simply takes "s.Centroid" as your new "centroids" array instead of adding it below the existing "centroids" array.
Reply to EDIT #3
In this part of your code
poly=polyfit(C1,C2,2); %'poly' is a vector of polynomial coefficients
g=roots(poly); %g solves for polynomial roots
v =max(g) %v is the largest root (assumed to be the expected ground-level destination)
plot(xPlot,polyval(poly,xPlot),'y') %plots polynomial at given x-values
%here, one expects to plot a point for the expected destination.
%the call to the plot function should follow syntax similar to the previous line
plot(v,'go') %plot function has syntax plot(x,y,options)
%therefore it should look like plot(v,polyval(poly,v),'go')
I've added some comments. The problem is with the line where you called plot
to plot the expected destination; the syntax seems to be wrong. If only 1 data argument is given, MATLAB assumes the given data to be the y-value, using its array index as the x-value (i.e. plotting y(1) at 1, y(2) at 2, etc). This is not what you want for variable v.
You can also use plot(v,0,'go')
as mentioned by Jonas, but it never hurts to double-check if the extrapolated polynomial value is actually near 0 ;)