Search code examples
matlabmatlab-figure

How to create an array with logical variables and iterate through those variables in a for loop?


I have 16 binary images of logic type, and I want to put those images (variable names) into an array and iterate through them in a for loop doing image processing.

Below is an example of my binary image names, and my current for loop (does not work).

bin_RD1 = imbinarize(rightDam1, T_RD1); %these are my binary images
bin_RD2 = imbinarize(rightDam2, T_RD2);
bin_RD3 = imbinarize(rightDam3, T_RD3);
bin_RD4 = imbinarize(rightDam4, T_RD4);

i = who('bin*'); %says of type 16x1 cell

for j = 1:length(i) %j is listed as just a number
k = i{j}; %char type: 'bin_RD1'
% logical k; did not work
roi = bwareaopen(k, 25); 
graindata = regionprops('table',roi,'Area','EquivDiameter','MajorAxisLength','MinorAxisLength','Centroid','Orientation'); 
end

Solution

  • Your wording is confusing and I don't know if this is what you're looking for. Try assigning your variables in matrix form (please keep in mind I've never worked with image manipulation so my indexing could be very wrong):

    for i=1:16
         bin_RD(:,:,i) = imbinarize(rightDam(:,:,i),T_RD(i));
    end
    

    You can use an operation like this to process your bin_RD variable as well. You wouldn't even have to leave the loop.

    for i=1:16
         bin_RD(:,:,i) = imbinarize(rightDam(:,:,i),T_RD(i));
         roi(:,:,i) = bwareaopen(bin_RD(:,:,i), 25);
         graindata = regionprops('table',roi(:,:,i),'Area','EquivDiameter','MajorAxisLength','MinorAxisLength','Centroid','Orientation'); 
    end
    

    One last piece of advice: I used i=1:16, but if you ever want to use this code again on a situation where you might have 5, 22, 100, etc images, use for i=1:length(T_RD) or something like that and you won't have to change it every time.