MATLAB: How to store the each values of x and y in the following loop so that i could plot graph x vs y

array storeimage processingMATLAB

for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
x=bc(1);
y=bc(2);
end

Best Answer

Change the two lines
x=bc(1);
y=bc(2);
to:
x(object)=bc(1);
y(object)=bc(2);
If 'stats' is large it is best to allocate space for x and y ahead of the for-loop:
x = zeros(length(stats),1);
y = zeros(length(stats),1);