How to replace a dataset with different dimensions in an .H5 file - hdf5lib2 error

9 vues (au cours des 30 derniers jours)
Teresa
Teresa le 31 Mai 2024
Réponse apportée : LeoAiE le 20 Juil 2024
I have a dataset comprising a cube of images, with dimension 256x320x151. I need tu cut it down to a dataset with dimension 256x320x10, while keeping all the other attributes invariate, as they have useful information on the data acquisition parameters.
I have tried the code:
h5disp('PCA_cube - Copia.h5')
fileattrib('PCA_cube - Copia.h5','+w');
fid = H5F.open('PCA_cube - Copia.h5','H5F_ACC_RDWR','H5P_DEFAULT');
dset_id = H5D.open(fid,'/Cube/Images');
H5D.set_extent(dset_id,[320,256,10]); % C-style indexing
H5D.close(dset_id);
H5F.close(fid);
h5read(PCA_cube - Copia.h5', '/Cube/Images')
However I get the error message: "Error using hdf5lib2 : The HDF5 library encountered an error and produced the following stack trace information:H5D__set_extent dataset has contiguous storage"
I'm adding also some information on the file structure:
Any help or suggestion is much welcome!

Réponses (1)

LeoAiE
LeoAiE le 20 Juil 2024
Hi,
It's hard to provide a good solution without a sample of the data to experiment with. Anyway here is a code you can adjust according to your needs.
% Define the file names
original_file = 'PCA_cube - Copia.h5';
new_file = 'PCA_cube_reduced.h5';
% Read the original dataset
data = h5read(original_file, '/Cube/Images');
% Select the slice you want (e.g., the first 10 images along the third dimension)
selected_data = data(:, :, 1:10);
% Create a new HDF5 file and write the selected data to it
h5create(new_file, '/Cube/Images', size(selected_data), 'Datatype', 'double');
h5write(new_file, '/Cube/Images', selected_data);
% Copy other attributes from the original file to the new file
info = h5info(original_file);
for i = 1:length(info.Groups)
group = info.Groups(i);
for j = 1:length(group.Datasets)
dataset = group.Datasets(j);
if ~strcmp(dataset.Name, 'Images') % Skip the 'Images' dataset as it's already handled
data = h5read(original_file, fullfile(group.Name, dataset.Name));
h5create(new_file, fullfile(group.Name, dataset.Name), size(data), 'Datatype', 'double');
h5write(new_file, fullfile(group.Name, dataset.Name), data);
end
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by