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)
Mostrar comentarios más antiguos
Sameer Pathan
el 11 de Abr. de 2019
Respondida: Sameer Pathan
el 11 de Abr. de 2019
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
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?
Respuesta aceptada
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
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.
Más respuestas (1)
Ver también
Categorías
Más información sobre Spreadsheets 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!