Borrar filtros
Borrar filtros

How to find average of all maltab files in folder and write avrage of each file in single excel file.

1 visualización (últimos 30 días)
Hi All,
I am new on maltab scripting, I have 100+ maltab files and all files having same structure. Maltab file containing only one single column and rows count will differ for each file, I need to find average of the each file and write into one excel file. Is possible to write average of file with file name in excel? I have tried below code but it having some error. Cloud please help me to achieve this requirement.
Thanks a lot!!
folder = 'D:\Your\Folder\'; % Give the folder path
matFiles = dir(fullfile(folder, '*.mat')); % Use absolute path names
numfiles = length(matFiles);
average = zeros(1, numfiles);
for k = 1:numfiles
M = load(folder(k).name);
average(k) = mean(M(:,1));
end
csvwrite(fullfile(folder, 'Avg_matFiles.csv'), average);
  2 comentarios
Guillaume
Guillaume el 11 de Abr. de 2019
If you get an error then give us the full text of the error message so we can understand what is going wrong.
If you load a mat file, typically you get a structure whose fields are the names of the variables inside the mat file. You only get a matrix if the file is a text file. Are you sure that M is a matrix?
Sameer Pathan
Sameer Pathan el 11 de Abr. de 2019
Below error i am getting and i could not understant what's the mean of this,
>>aap
struct contents reference from a non-struct array object.
Error in aap (line 6)
M = load(folder(k).name);

Iniciar sesión para comentar.

Respuesta aceptada

Bob Thompson
Bob Thompson el 11 de Abr. de 2019
You are receiving that error because you are trying to load the directory string, not the file.
M = load(matFiles(k).name);
  5 comentarios
Sameer Pathan
Sameer Pathan el 11 de Abr. de 2019
It looks like some thing this,
folder = 'D:\Your\Folder\'; % Give the folder path
matFiles = dir(fullfile(folder, '*.mat')); % Use absolute path names
numfiles = length(matFiles);
average = zeros(1, numfiles);
for k = 1:numfiles
[results(k).name] = matFiles(k).name;
[results(k).average] = mean(M.M(:,1));
end
csvwrite(fullfile(folder, 'Avg_matFiles.csv'), results);
Am I correct?
Bob Thompson
Bob Thompson el 11 de Abr. de 2019
I apologize, I forgot that you were just wanting to write to a .csv file. In that case the best way to do strings and data is to do a table. You can use your original average(k) method as well.
folder = 'D:\Your\Folder\'; % Give the folder path
matFiles = dir(fullfile(folder, '*.mat')); % Use absolute path names
numfiles = length(matFiles);
average = zeros(1, numfiles);
for k = 1:numfiles
average(k) = mean(M.M(:,1));
end
results = table('Name',[matFiles.name],'Average',average)
writetable(results,fullfile(folder, 'Avg_matFiles.csv'));
I am specifically switching to writetable instead of csvwrite, because csvwrite only works with numeric data, while writetable is not limited in such a manner.

Iniciar sesión para comentar.

Más respuestas (1)

Sameer Pathan
Sameer Pathan el 11 de Abr. de 2019
Thanks a lot, you are the superb!!!

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by