How do I store data in a table from a for loop?

Dear Matlab-Community,
I am working on a code to analyze images. The code so far works really well, but I can not seem to find out how to continue.
How I want the code to work is reading in multiple images and detect the area of the binarized image. Everything works fine but since I am reading in at least 20 images, the goal is to save each data from each loop from the "diameter" calculation.
Can anyone help a desperate student?
This is the code i wrote so far (please don't judge, I just started like 3 weeks ago):
% clc;
source = uigetdir([]);
d = dir(fullfile(source, '*.tif'));
x_axis = 142.7; %Microns
Resolution_x = 1264; %Pixels
micron = x_axis/Resolution_x ; %Microns/Pixel
%%
% Calculating Droplet Size Distribution
for i = 1:length(d)
read_filename = [num2str(i),'.tif'];
rgb = imread(fullfile(source, read_filename));
%Extracting green color map and converting it to gray scale
gmat = rgb(:,:,2);
gray_g = mat2gray(gmat);
%Set max contrast in graymaps
gray_g_enh = imadjust(gray_g,stretchlim(gray_g),[]);
%Binarize grayscale and morphological commands
g_bw = imbinarize(gray_g_enh,'global');
g_morphed = bwmorph(g_bw,'majority',Inf);
g_morphed1 = bwmorph(g_morphed,'fill',Inf);
bw_filled = imfill(g_morphed1,'holes');
stats = regionprops(logical(bw_filled),'area');
stats_array = struct2array(stats);
area = micron*stats_array;
diameter = sqrt((area*4)/pi);
end

 Akzeptierte Antwort

Star Strider
Star Strider am 3 Okt. 2020

0 Stimmen

I do not follow everything you are doing, however if you simply want to save the ‘diameter’ result:
diameter(i) = sqrt((area*4)/pi); % Scalar Result
diameter{i} = sqrt((area*4)/pi); % Non-Scalar Result
Note the curly brackets {} in the second option, denoting a cell array. If you are not familiar with cell arrays, see Access Data in Cell Array and related documentation for more information.

4 Kommentare

Hi Star Strider,
thank you for your fast reply.
I am sorry if I didn't state my problem clear enough. I am calculating diameters of droplets found in the read in images. However, the number of the total calculated diameters vary each iteration because not every image contains the same number of droplets. Therefore I would like to store the results from each iteration in a table or matrix (I dont know which is better). For a better statistical results I then would like to take the results from the saved data and plot them all together in a histogram and in a cumulative distribution figure (which I know how to do)
The diameter-calculation creates a 1xYYY - table each iteration. Where YYY is the number of calculated diameters.
Is it possible to store the results like this:
Row 1 = Diameters from image 1
Row 2 = Diameters from image 2
...
Row 20 = Diameetrs from image 20
My pleasure!
The second option, using the cell array, will likely do what you want, since it can store different numbers of elements in each element of the cell array. After that, you can do what you want with the contents of the cell array. For that reason, I included the link on accessing cell array data.
One option after you collect them is to concatenate all the saved diameters in a single vector, then process them. There are different ways to do that with each element of the cell array, the cell2mat function probably being the easiest.
Good lord are you serious? I just had to add like 3 charakters? I worked on that like forever.
Thank you so much!! It finally works!!!
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by