MatLab: Average (column,row) entry across many different files -> create output file

Hi there,
I have many files with an".IV0" ending (this is something similiar to .txt). In each file I have an header of 15 rows. This header should not be aken into account. After the header there are 3 columns (seperated by " " and about 81 rows). What I want to do now is to average e.g. row "16" column "1" of each .IV0 file and make an outputfile, which has 3 columns and about 81 rows and in each (row, column) is the average of all the same (row, column) elements from all the different files.
I have tried what you have said, but don´t really know what to do anymore...Can you help me? This is the code I tried:
folder = 'C:\Users\Averager\';
IV0Files = dir(fullfile(folder, '*.IV0')); % Use absolute path names
numfiles = length(IV0Files);
average1 = zeros(1, numfiles);
average2 = zeros(1, numfiles);
average3 = zeros(1, numfiles);
for k = 1:numfiles
M = csvread(fullfile(folder, IV0Files(k).name));
average1(k) = mean(M(:,1));
%average2(k) = mean(M(:,8));
end
iv0write(fullfile(folder, 'output.IV0'), average1);

2 comentarios

so, do you have some data files to share ?
I am on
Hi Mathieu,
yes I do have some data (attached). I had to convert them to a txt file in order to upload it. It shouldn´t make a difference, if it is a .IV0 file or a .txt file in the end. i guess.
I already worked on it again and created this:
folder = 'C:\Users\Averager\';
folderout = 'C:\Users\Auswertung\';
IV0Files = dir(fullfile(folder, '*.IV0')); % Use absolute path names
numfiles = length(IV0Files);
%the following should be automatic. I did it manual for two examples to see if it is working in general
M = load(IV0Files(1).name);
A = load(IV0Files(2).name);
average = (M+A)/numfiles;
%this is to save the data in the new IV0 file
iv0write(fullfile(folderout, 'output.IV0'), average);

Iniciar sesión para comentar.

 Respuesta aceptada

So this is my suggestion
I wonder why we need to average also the 3rd column with is the time values ?? making not much sense IMHO
IV0Files = dir(fullfile(folder, '*.txt')); % Use absolute path names
numfiles = length(IV0Files);
data_sum = 0;
for ci = 1:numfiles
data_file = readmatrix(IV0Files(ci).name,'NumHeaderLines',15);
data_sum = data_sum+data_file; % summation accross the files
end
% divide by numfiles to have the average (and not the sum)
average = data_sum/numfiles;
% %this is to save the data in the new IV0 file
writematrix(average, fullfile(folderout, 'output.txt'))

4 comentarios

Hi Mathieu,
this solution is great and it works!!! :)
But I have one more problem: Since my file is a .IV0 and not a .txt I get the following error. I tried to fix it with this opts variable, but I don´t really know how...
So i thought maybe first converting the .IV0 files into .txt files and do the process and the make an .IV0 output file.
I need an .IV0 output since my program afterwards is based on this datafiletype.
Aaan yes with the time you are right, but I don´t need the time anyway and I thought easiest way to process the data is, to just average this column too.
Error using readmatrix (line 157)
The file extension '.IV0' is not a known file extension for 'text' or 'spreadsheet' file types.
To specify the file type, use 'FileType' with 'text' or 'spreadsheet'. For example:
opts = detectImportOptions(filename, 'FileType', 'spreadsheet')
Error in averagermat (line 7)
data_file = readmatrix(IV0Files(ci).name,'NumHeaderLines',15);
this is an improved version
  • tab delimited output file
  • time is removed
  • IV0 extension accepetd both for reading and saving
folder = cd; % 'C:\Users\Averager\';
folderout = [cd '\out']; %'C:\Users\Auswertung\';
IV0Files = dir(fullfile(folder, '*.IV0')); % Use absolute path names
numfiles = length(IV0Files);
data_sum = 0;
rows = [1,2];
for ci = 1:numfiles
data_file = readmatrix(IV0Files(ci).name,"NumHeaderLines",15,"FileType","text");
data_sum = data_sum+data_file(:,rows); % summation accross the files (removed 3rd column (time))
end
% divide by numfiles to have the average (and not the sum)
average = data_sum/numfiles;
% %this is to save the data in the new IV0 file
writematrix(average, fullfile(folderout,'output.IV0'),"Delimiter","tab", "FileType", "text");
IT WORKS!!! YEAH. Great. Thanks a million Mathieu.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 10 de Dic. de 2020

Comentada:

el 11 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by