MATLAB Answers

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

2 views (last 30 days)
emmanouil alexakis
emmanouil alexakis on 24 Nov 2020
Answered: Mathieu NOE on 24 Nov 2020
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!

  0 Comments

Sign in to comment.

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

  0 Comments

Sign in to comment.

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

  0 Comments

Sign in to comment.


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

  0 Comments

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!