How to automate reading multiple HDF files and saving the information from it?
4 views (last 30 days)
Show older comments
Hello!
I currently have a folder with 15.000 HDF files all named YYYYMMDD.HDF from data since 1997. So the first file is 19970101.HDF and the last one is 20203131.HDF
Each of these files has a matrix I need to save the values of (precipitation values)
With
hdfread("19970101.HDF","precipitation");
I can exctract the matrix for one of these files, which gets saved as ans.mat in my workplace.
Ideally I need a way to read all these HDF files and have them saved with their original name but in .mat, so 19970101.mat
I've tried plenty of things for the last 3 days but I can't come up with how to automate this process. I appreciate any help I can get.
Thanks in advance.
0 Comments
Answers (1)
Vatsal
on 1 Mar 2024
Hi,
Automating the reading of HDF files and saving the extracted information as .mat files with their original names can be achieved by writing a MATLAB script.
Below is an example illustrating how to implement this process:
% Define the folder where your HDF files are located
folderPath = 'path_to_your_folder'; % Replace with your folder path
% Get a list of all HDF files in the folder
hdfFiles = dir(fullfile(folderPath, '*.HDF'));
% Loop through each HDF file
for k = 1:length(hdfFiles)
% Construct the full file path
hdfFilePath = fullfile(folderPath, hdfFiles(k).name);
% Read the precipitation matrix from the HDF file
precipitation = hdfread(hdfFilePath, 'precipitation');
% Construct the new file name by replacing the HDF extension with MAT
matFileName = strrep(hdfFiles(k).name, '.HDF', '.mat');
% Construct the full path for the new .mat file
matFilePath = fullfile(folderPath, matFileName);
% Save the precipitation matrix to a .mat file
save(matFilePath, 'precipitation');
end
% Display a message when the process is complete
disp('All files have been processed and saved as .mat files.');
I hope this helps!
0 Comments
See Also
Categories
Find more on HDF5 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!