Loop on loading files

Hi!
I have a folder containing subfolders containing hdf5 files (10,000 of them in fact).
A example of a path would be 'data\A\F\G\TRATTGW128F427A82D.h5'.
Below 'data', there are 3 levels of folders, all named by a single letter. The final files all have similar names to TRATTGW128F427A82D.h5.
I have a function that converts hdf5 files to .mat. I want to upload all these files and store some of their info in a matrix.
Example:
hdf5_to_matfile('path\to\hdf5.h5','path\to\outputk.mat');
matk=load('path\to\outputk.mat');
A(k,1)=matk.data1;
A(k,2)=matk.data2;
How can I loop over all my folders and subfolder to obtain this one single matrix A containing one line for each folder with data1 and data2 in each column? And what would be the fastest way to proceed?
Thank you for your help!

추가 답변 (1개)

Jeff
Jeff 2013년 5월 2일

0 개 추천

Thank you! This link really helped. However I have one last problem. I can get all the .h5 in one single directory, however I want my program to go through all the lower directories as well. The beginning of my code is:
myFolder = 'D:\Documents\Subset\A\A\A\';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.h5');
but I also want the files that are in 'D:\Documents\Subset\A\A\B\*.h5', 'D:\Documents\Subset\A\B\F\*.h5' or 'D:\Documents\Subset\B\G\D\*.h5' for example. In my case, there are 1352 combinations in total. I tried doing:
myFolder = 'D:\Documents\MillionSongSubset\';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*\*\*\*.h5');
but it did not work... Is there another trick I need to know here?

댓글 수: 2

Jeff
Jeff 2013년 5월 2일
In fact, I want to know how I can go through all the subdirectories of 'D:\Documents\Subset\' recursively.
Walter Roberson
Walter Roberson 2013년 5월 2일
You posted code for this somewhere along the line. Or you can look in the File Exchange for rdir or another rdir

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by