Clear Filters
Clear Filters

how to average multiple folders with multiple files. I am attaching three folders with two files each. I need a help to find an average of two files from three folders. My code is showing an error"Dot indexing is not supported for variables "

2 views (last 30 days)
close all
clear all
D = 'D:\filename\';
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
for ii = 1:numel(N)
T = dir(fullfile(D,N{ii},'*')); % improve by specifying the file extension.
C = {T(~[T.isdir]).name}; % files in subfolder.
for jj = 1:numel(C)
F = fullfile(D,N{ii},C{jj})
fprintf('test%s\n',F);
S(ii).data = F(:);
end
end
Y = cat(3,S(:))%this line is wrong
out = mean(Y,3)
  3 Comments
MS
MS on 30 Mar 2020
Thanks. I need two files(test_001.txt, test_002,txt) from all three folders(out of six files) finally. let me know if you need further clarifications.
MS
MS on 30 Mar 2020
code through N(here 3 folders) different folders with M(two files here) different.txt files(text​001.txt...​..text002.​txt) with (O rows and P columns). and find the average(te​xt001.txt.​....text002.txt)of M different files from the N folders.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 30 Mar 2020
Create a script in the same directory where you have folders T1, T2, and T3. Then paste the following code in that script and run it
files = dir('**/*.txt');
data = cell(1, numel(files));
for i=1:numel(files)
filename = fullfile(files(i).folder, files(i).name);
data{i} = readmatrix(filename);
end
M = cat(3, data{:});
average_val = mean(M, 3);
  22 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!