How do I store data in a table from a for loop?
3 views (last 30 days)
Show older comments
Denis van Egeren
on 3 Oct 2020
Commented: Star Strider
on 3 Oct 2020
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
0 Comments
Accepted Answer
Star Strider
on 3 Oct 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Logical 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!