How can I read multiple mat files and perform ttest2 function?

2 views (last 30 days)
hi!
I have 108 mat files and I must go through them and test them by 2. The thing is, I haven't find a way to include all of them, without having to call them seperetaly,and at the same time storing the results for each comparison.( i am doing a ttest for each pair of files). Obvious a loop will be the answer but haven't any idea how to put the files in the loop.
Thank you!

Accepted Answer

dpb
dpb on 24 Nov 2020
Edited: dpb on 24 Nov 2020
Arrange the name convention so they will sort in the desired sequence and can process by twos -- then
d=dir(fullfile('directory','appropriatename*.mat')); % return the list of files
for i=1:2:numel(d)
data1=importdata(fullfile(d(i).folder,d(i).name));
data2=importdata(fullfile(d(i+1).folder,d(i+1).name));
% do whatever with the two files here before going on...save the results wanted
end

More Answers (2)

KSSV
KSSV on 24 Nov 2020
This question has been discussed to death. A simple search will give you lot of links.
matFiles = dir('*.mat') ;
N = length(matFiles) ;
for i = 1:N
thisFile = matFiles(i).name ;
load(thisFile) ;
% Do what you want
end

Mathieu NOE
Mathieu NOE on 24 Nov 2020
hello
my single for loop code example
enjoy
n = 108;; % number of files
s = 2; % create combination of 2 elements
[comb,nComb]=makecomb(n,s) % combinations
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This functions evaluates all possible combinations of N elements
% taken by sets of S elements.
% Usage: [comb,nComb]=makecomb(number_of_elements,size_of_each_combination);
% Return values:
% comb: a S x nComb matrix where each column of S rows contains a possible
% combination of N elements;
% nComb: number of evaluated combinations (and number of columns of comb);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for ci = 1:nComb
file1 = ['file' num2str(comb(1,ci))];
file2 = ['file' num2str(comb(2,ci))];
% your code here
end

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!