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

1 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2019-4-11
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 2019-4-11
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);

请先登录,再进行评论。

采纳的回答

Bob Thompson
Bob Thompson 2019-4-11
You are receiving that error because you are trying to load the directory string, not the file.
M = load(matFiles(k).name);
  5 个评论
Sameer Pathan
Sameer Pathan 2019-4-11
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 2019-4-11
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.

请先登录,再进行评论。

更多回答(1 个)

Sameer Pathan
Sameer Pathan 2019-4-11
Thanks a lot, you are the superb!!!

Community Treasure Hunt

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

Start Hunting!

Translated by