MATLAB Answers

Getting the average and then the standard deviation of multiple .mat files

3 ビュー (過去 30 日間)
Ben Timm
Ben Timm 2020 年 4 月 28 日
コメント済み: Ben Timm 2020 年 5 月 7 日
Hi all,
Slightly over my head here and need some help! I am trying to get the average of several .mat files, each being 8x22 workspaces of data (see the screenshots for more information). My code is below but despite my efforts, I have no idea how to get the average of each file as well as their standard deveation. As you can see, I have loaded them all through the following process but have gotten stuck with the averaging part for now. Could anyone help?
myFolder = '/Users/ben/Desktop/Stats part/Data'; % Define your working folder
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
matData(k) = load(fullFileName);
% the loaded data is in the workspace called matData.
Z = rand(1,16,16);
Z_m = mean(A(1:20,:,:));
Thanks in advance!!

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2020 年 4 月 29 日
"see the screenshots for more information"
You forgot to attach any files.
"each being 8x22 workspaces of data"
Do all of the .mat files contain the same variables? Which of these variables do you want to work with?
Ben Timm
Ben Timm 2020 年 4 月 29 日
Ahh! That was a rookie error, my bad!
All of the .mat files have different numbers in them but aside from that, they are of the same format and do not change at all from what is seen in the screenshots.
Thanks again!

Sign in to comment.


Vinai Datta Thatiparthi
Vinai Datta Thatiparthi 2020 年 5 月 1 日
Hey Ben,
From your description, I understood that you're trying to evaluate the mean and standard deviation values for each of the 16 matrix values that you have inside your struct matData. So, your output results would be 16 values for mean & 16 more for standard deviation.
If that's the case,
% Number of elements within the structure matData
len = numel(fieldnames(matData));
% Array to store average of every matrix
meanArr = zeros(1, len);
% Array to store standard deviation of every matrix
stdArr = zeros(1, len);
% Iterate through every element (matrix) within matData to calculate mean & std
for idx = 1:len
data = matData(idx).average_final_results;
% Store into array
meanArr(idx) = mean(data,'all');
stdArr(idx) = std(data, 0, 'all');
Documentation links for mean & std.
Note: Since you've not mentioned what exactly you're calculating std for, I've gone ahead and assumed a simple case and calculated for the entire matrix.
Hope this helps!

  5 件のコメント

表示 2 件の古いコメント
Ben Timm
Ben Timm 2020 年 5 月 5 日
Definitely! You're an angel, thank you!
I will look into the normplot function now, would it be possible to get that distribution from the array you created?
Kind regards,
Vinai Datta Thatiparthi
Vinai Datta Thatiparthi 2020 年 5 月 6 日
Hey Ben,
I guess using normplot on the variable data is what you're looking for. Since you wanted to check if each individual group was normally distributed, maybe you could use normplot iteratively within the for loop itself.
Hope this helps!
Ben Timm
Ben Timm 2020 年 5 月 7 日
Hi Vinai,
Thank you so much for the help! It is incredibly appreciated.
Kind regards,

Sign in to comment.

その他の回答 (0 件)

Translated by