Azzera filtri
Azzera filtri

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 visualizzazioni (ultimi 30 giorni)
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 Commenti
MS
MS il 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 il 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.

Accedi per commentare.

Risposta accettata

Ameer Hamza
Ameer Hamza il 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 Commenti

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations 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!

Translated by