How to calculate the mean/median/Standard deviation for each column in a table

364 visualizaciones (últimos 30 días)
Here I have table has 716rows*9 coloumns called Analysis1 as shown in figure1.
I would like to each the following steps:
  1. The calcualtions for n number of files like this in a folder
  2. The formula should be calculated for each coulmn (not for rows) and all the values shoul be store in a one seperate file so that I can plot a graph
I have tried the formula using median(Analysis1) but showing the permute error as shown in figure 2.
I have suceeded applying the formula for an individual coulumn like as shown in figure2:
  1. Result1 = median(Analysis.Time)
  2. Result2 = median(Analysis.TempL1)
Any suggestion would be appreciated to solve this issue. Thanks in advance.

Respuesta aceptada

Steven Lord
Steven Lord el 4 de Dic. de 2020
Let's look at a sample table.
load patients
T = table(LastName, Smoker, Height, Weight, Systolic, Diastolic);
head(T)
ans = 8x6 table
LastName Smoker Height Weight Systolic Diastolic ____________ ______ ______ ______ ________ _________ {'Smith' } true 71 176 124 93 {'Johnson' } false 69 163 109 77 {'Williams'} false 64 131 125 83 {'Jones' } false 67 133 117 75 {'Brown' } false 64 119 122 80 {'Davis' } false 68 142 121 70 {'Miller' } true 64 142 130 88 {'Wilson' } false 68 180 115 82
It makes sense to compute the mean for four variables in this table, but not for the LastName or Smoker variables.
M = varfun(@mean, T, 'InputVariables', @isnumeric)
M = 1x4 table
mean_Height mean_Weight mean_Systolic mean_Diastolic ___________ ___________ _____________ ______________ 67.07 154 122.78 82.96
This looks for all the variables in T for which isnumeric returns true and calls mean on those variables.

Más respuestas (1)

Image Analyst
Image Analyst el 4 de Dic. de 2020
Did you try something like this (untested)
m = table2array(yourTable); % Convert table to regular matrix.
columnMeans = mean(m, 1); % Get the mean of each column in the matrix.
  11 comentarios
Image Analyst
Image Analyst el 8 de Dic. de 2020
If you want only one workbook for all the results, do this:
% Specify the folder (directory) of CSV files that you want to process.
myFolder = '/Users/xxxxxxx/Desktop/Tests/27.11.2020 csv/Total';
% Get a wildcard pattern so we can get a list of all CSV files in the folder.
filePattern = fullfile(myFolder, '*.csv');
% Below is the code for looping over all the files, reading them in,
% computing the column means, and writing that to an output workbook
% (one output .XLSX file for each input .CSV file).
theFiles = dir(filePattern); % This is a structure.
% Make a 2-D array to hold all the column means
columnMeans = zeros(9, length(theFiles)); % One column for each file.
for k = 1: length(theFiles)
baseFileName = theFiles(k).name;
fullInputCSVFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Processing %s...\n', fullInputCSVFileName);
% Read this matrix from the csv file.
thisMatrix = dlmread(fullInputCSVFileName,',',1,2);
% Get the means of each column.
theMeans = mean(thisMatrix,1);
% Convert from a row vector to a column vector because that's how he wants the output file.
columnMeans(:, k) = theMeans'; % Add this column vector to the 2-D array.
end
% Create a name for the output Excel workbook.
baseFileName = 'Column Means.xlsx'; % Whatever you want to call it
% Prepend the folder to get the full file name.
fullWorkBookFileName = fullfile(myFolder, baseFileName);
% Write the output workbook.
fprintf('Writing output Excel workbook : %s...\n', fullWorkBookFileName);
writematrix(columnMeans, fullWorkBookFileName)
% Alert user that we're done.
message = sprintf('Done processeing %d CSV files.\n', length(theFiles));
uiwait(msgbox(message));
Nagamani Yaadavalli
Nagamani Yaadavalli el 8 de Dic. de 2020
I tried with this code !! The previous csv files data also changing. So that I tried the below code from your suggesstions. It works great, Thanks for all the suggestions. The output as shown in figure !!
% getting the folder directory of files you want to work
myFolder = '/Users/xxxxxxx/Desktop/xxxxxx/Tests/27.11.2020 csv/Total';
% code for determining the file pattern that you have, * represents all the
% files
filePattern = fullfile(myFolder, '*.csv');
% code for addressing all the files
theFiles = dir(filePattern)
for k = 1: length(theFiles)
baseFileName = theFiles(k).name;
fullfilename = fullfile(theFiles(k).folder,baseFileName)
fprintf(1,fullfilename);
Test= dlmread(fullfilename,',',1,2)
theMeans = mean(Test,1)
end
Analysis1(:,k) = theMeans'

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by