Yannis_K
Yannis_K

Reputation: 7

How to make an array of .mat files and append it with a for

I have that code,

h1 = dir('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)'); %angry
h2 = dir('C:\Users\John\Documents\MATLAB\code for yannis\neutral(N)');%neutral
h3 = dir('C:\Users\John\Documents\MATLAB\code for yannis\happiness(F)');%happy

%fprintf('%s', filename);%filename h(i,1).name
fprintf('%d \n',numel(h1));
fprintf('%d \n',numel(h2));
fprintf('%d \n',numel(h3));

%fprintf('%d', max(numel(h1),numel(h2),numel(h3)));

A= [numel(h1) numel(h2) numel(h3)];

fprintf('%d \n \n \n', max(A));
fprintf('%s \n', h1(2).name);
%load('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\*.mat');
fprintf('%d \n \n \n', length(h1));
resultsdir = 'results';
addpath('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
array = [h1(2)];
for i=3:max(A)+3
    %s= 'C:\Users\John\Documents\MATLAB\code for yannis\anger(W)';
    thisfile = h1(i).name;
    destfile = fullfile(resultsdir, thisfile);
   thisdata = load(thisfile);

   %array[thisdata];

   %strcmp(h(i,1).name(1:2), s);
    %cat(1, array, h1(i,1).M);
    fprintf('%s \n', h1(i,1).name);
end

and i want to keep all the .mat files in an array in order to compare it with other mat files Thanks in advance

Upvotes: 0

Views: 144

Answers (2)

sco1
sco1

Reputation: 12214

You can select the top level folder and utilize your system's dir call to recursively search for all files that match your criteria (*.mat, in this case)

For example:

mypath = uigetdir('', 'Select Top Level Folder');
oldpath = cd(mypath);  % cd to data directory for simpler dir call
[~, cmdout] = system('dir /S /B *.mat');
cd(oldpath);  % Return to previous path
mymatfiles = regexp(cmdout, '(.:\\[\w\-\\. ]+\.\w+)', 'match');

The system call returns one long string with all of the absolute paths to your *.mat files. I utilize a regexp to split this into a cell array, where each cell is an absolute path to a single *.mat file.

Note that this is Windows only because it uses the MS-DOS dir command. This function can easily be adapted to other operating systems, I just don't know them.

Upvotes: 1

Suever
Suever

Reputation: 65430

Assuming you have .mat files in all of those directories (ending in ".mat"), simply get the directory listings of each of those directories and store them.

% All of the folders where the *.mat files live
folders = {'C:\folder1'; 'C:\folder2'};

allfiles = cell();

for k = 1:numel(folders)
    % Find all the .mat files
    files = dir(fullfile(folders{k}, '*.mat'));

    % Convert to absolute paths
    fullpaths = cellfun(@(x)fullfile(folders{k}, x), {files.name}, 'uniform', 0);

    % Store in our global cell array
    allfiles = cat(1, allfiles, fullpaths(:));    
end

Now allfiles contains an entry for every .mat file contained within those paths. You can then loop through this cell array to perform any operations you need.

alldata = cellfun(@load, allfiles, 'uniform', 0);

Upvotes: 0

Related Questions