titus.andronicus
titus.andronicus

Reputation: 517

Elegant way to get the number of .mat files in a directory

Assuming I have X .matfiles under a directory called my_experiment_1/data, I know I can load them into

experiment1_files = dir(['my_experiments/data/*.mat']);

Now I would like to open them in a for loop with the .name extension of dir:

for count = 1:N
  % load data-set
  load(experiment1_files(count).name);
  ...
end

and perform a bunch of operations with the matrices of each file.

Question: what is the way to compute the number of files withing a directory in MATLAB (meaning, the number N in the for-loop above)?

Upvotes: 0

Views: 1187

Answers (1)

Maurits
Maurits

Reputation: 2114

As stated in the specification of dir it returns an Nx1 struct where the number of items N corresponds to the number of files and folders it retrieved from the path you pass to dir.

Upvotes: 2

Related Questions