How do I save a variable in a for loop by using the same name but changing the number.

2 views (last 30 days)
Hello,
As you can see from my code below, I am getting an image of 5 cells and placing a bounding box around each one. After producing the bounding box, I simply wanted to crop each separate cell into its own image separate image labeled 'Crop_1', 'Crop_2', etc. In order to do that I need to save the variable within each loop in order for it to not be overwritten by the next loop. Currently, my code will only crop the image of the last loop run.
So in other words, how do I write code within a loop to save the output of the loop as a variable and just simply making the variable name Crop_(i) with i representing the specific number cell.
Thank you in advanced
for i = 1:length(stats)
Cell_i = stats(i).BoundingBox;
rectangle('Position', [Cell_i(1),Cell_i(2),Cell_i(3),...
Cell_i(4)],'EdgeColor',cstring(mod(i,5)+1),'LineWidth',2);
disp(['Cell ', num2str(i), ' : ',num2str(stats(i).Area)]);
end
crop_img = imcrop(final_img,Cell_i);
imshow(crop_img)
imwrite(crop_img,'cell1.tif')

Answers (2)

Walter Roberson
Walter Roberson on 18 Apr 2014

Image Analyst
Image Analyst on 18 Apr 2014
See my tutorial where I extract/crop subimages that have blobs of interest (coins) in them: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!