looping across different mat file names
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Image Analyst
2022-4-8
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 个评论
Image Analyst
2022-4-8
编辑:Image Analyst
2022-4-8
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!