looping across different mat file names
1 view (last 30 days)
Show older comments
I have six mat files with different names. I want to load them and perform image processing opeations and store the result in table.
I wrote this code for two of them. How can I load them and work on them using loop. the method I am using now loads the file but either in structure or cell array
clc;
clear ;
close all;
%load the data cropped
load('APP1.mat');load('WT1.mat');
% define the edges and the bin size and plot the histogram
edges=(20000:1000:50000);
APP1=nonzeros(double(APP1(:)));
WT1=nonzeros(double(WT1(:)));
histogram(APP1(:),'Binwidth',50,'BinEdges',edges,'FaceAlpha',.3);
hold on
histogram(WT1(:),'Binwidth',50,'BinEdges',edges,'FaceAlpha',.3);
legend('APP1','WT1')
%%
[pixelCounts1, Binedges1] = histcounts(APP1(:),'Binwidth',50,'BinEdges',edges);% bin edges range from 20000 to 50000 and the spacing of 1000
[pixelCounts2, Binedges2] = histcounts(WT1(:),'Binwidth',50,'BinEdges',edges);
%%
centers1 = Binedges1(1:end-1) + diff(Binedges1) / 2; % to reach the centers of the bin because histogram with 3 bins will have four edges abd that is why you will get an array of one size more than the pixel counts
centers2 = Binedges2(1:end-1) + diff(Binedges2) / 2;
figure;plot(centers1,pixelCounts1,centers2,pixelCounts2)
legend('APP1','WT1')
%% Get the number of pixels in the histogram.
numberOfPixels1 = sum(pixelCounts1);
numberOfPixels2 = sum(pixelCounts2);
%%
meanAPP1 = sum(centers1 .* pixelCounts1)/ numberOfPixels1;
meanWT1 = sum(centers2 .* pixelCounts2)/ numberOfPixels2;
%% Get the variance,
varianceAPP1 = sum((centers1 - meanAPP1) .^ 2 .* pixelCounts1) / (numberOfPixels1-1);
varianceWT1 = sum((centers2 - meanWT1) .^ 2 .* pixelCounts2) / (numberOfPixels2-1);
% Get the standard deviation.
stdDevAPP1 = sqrt(varianceAPP1);
stdDevWT1 = sqrt(varianceWT1);
% % Get the skew.The formula given in most textbooks is Skew = 3 * (Mean – Median) / Standard Deviation.
skewAPP1 = sum((centers1 - meanAPP1) .^ 3 .* pixelCounts1) / ((numberOfPixels1 - 1) * stdDevAPP1^3);
skewWT1 = sum((centers2 - meanWT1) .^ 3 .* pixelCounts2) / ((numberOfPixels2 - 1) * stdDevWT1^3);
% % Get the kurtosis. Kurtosis = Fourth Moment / Second Moment2
kurtosisAPP1 = sum((centers1 - meanAPP1) .^ 4 .* pixelCounts1) / ((numberOfPixels1 - 1) * stdDevAPP1^4);
kurtosisWT1 = sum((centers2 - meanWT1) .^ 4.* pixelCounts2) / ((numberOfPixels2 - 1) * stdDevWT1^4);
Fishtype={'APP1','WT1'}';
Means = [meanAPP1,meanWT1]';
Variance=[varianceAPP1,varianceWT1]';
standard_deviation =[stdDevAPP1,stdDevWT1]';
Skewness_1=[skewAPP1,skewWT1]';
Kurtosis_1= [kurtosisAPP1,kurtosisWT1 ]';
T=table(Fishtype,Means,Variance,standard_deviation,Skewness_1,Kurtosis_1);
0 Comments
Answers (1)
Image Analyst
on 8 Apr 2022
Are there only the 6 .mat files that you need in the folder, and no others? Or else do you have some way to specify only the 6 that you need?
Have you seen the FAQ:
2 Comments
Image Analyst
on 8 Apr 2022
Edited: Image Analyst
on 8 Apr 2022
You need to index your variables and then use (:) when you use them to build a table. For example
stdDevAPP1(j) = sqrt(varianceAPP1);
then, after the loop
T = table(Fishtype(:), Means(:), Variance(:), standard_deviation(:), Skewness_1(:), Kurtosis_1(:), ...
'VariableNames', {'FishType', 'Mean', 'Variance', 'StDev', 'Skewness', 'Kurtosis', });
Attach your 6 mat files if you need more help.
See Also
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!