How to find average of all maltab files in folder and write avrage of each file in single excel file.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Sameer Pathan
il 11 Apr 2019
Risposto: Sameer Pathan
il 11 Apr 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 Commenti
Guillaume
il 11 Apr 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?
Risposta accettata
Bob Thompson
il 11 Apr 2019
You are receiving that error because you are trying to load the directory string, not the file.
M = load(matFiles(k).name);
5 Commenti
Bob Thompson
il 11 Apr 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.
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!