looping across different mat file names

조회 수: 2 (최근 30일)
Rimple Poonia Dabas
Rimple Poonia Dabas 2022년 4월 8일
편집: Image Analyst 2022년 4월 8일
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);

답변 (1개)

Image Analyst
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
Rimple Poonia Dabas
Rimple Poonia Dabas 2022년 4월 8일
For now I only have 6.mat files in the folder. These are cropped images data for analysis I am doing to differenciate between two groups based on histogram analysis. I have taken help from your imagecapturemoments demonstration. They are named based on the groups.
Here I am trying to do on one group but I am having trouble doing it for all the three from this group.
clc;
clear ;
close all;
n=3;
for j=1:n
temp=load(sprintf('APP%d.mat',j));
image(j)= struct2cell((temp));
data=image{1,j};
newdata = nonzeros(double(data(:)));
edges=(20000:1000:50000);
histogram(newdata(:),'Binwidth',50,'BinEdges',edges,'FaceAlpha',.3);
[pixelCounts1, Binedges1] = histcounts(newdata(:),'Binwidth',50,'BinEdges',edges);
centers1 = Binedges1(1:end-1) + diff(Binedges1) / 2;
numberOfPixels1 = sum(pixelCounts1);
meanAPP1 = sum(centers1 .* pixelCounts1)/ numberOfPixels1;
varianceAPP1 = sum((centers1 - meanAPP1) .^ 2 .* pixelCounts1) / (numberOfPixels1-1);
stdDevAPP1 = sqrt(varianceAPP1);
skewAPP1 = sum((centers1 - meanAPP1) .^ 3 .* pixelCounts1) / ((numberOfPixels1 - 1) * stdDevAPP1^3);
kurtosisAPP1 = sum((centers1 - meanAPP1) .^ 4 .* pixelCounts1) / ((numberOfPixels1 - 1) * stdDevAPP1^4);
end
Image Analyst
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 CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by