Hi Georges,
To my understanding, you have generated a 1x2000 dimension cell array, with each cell being a matrix of size 21x512. Now, you want to save the cell array to an H5 file to use it in python; however, you are getting a 1x2000 double array with all zeroes in the h5 file.
Upon investigating, I found that the cell array needs to be converted to a 3D numeric array with dimension 21 x 512 x 512, as the H5 file stores the numeric arrays, and not cell arrays. Finally, the generated 3D arrays, both for test and train data, can be passed to “h5create” and “h5write” functions to save data to the H5 file.
Below is the updated MATLAB code with all the required modifications:
% Convert cell arrays to 3D arrays
x_train_array = cat(3, x_train{:});
x_test_array = cat(3, x_test{:});
savefileh5 = 'traintest.h5';
% Save x_train
try
h5create(savefileh5, '/train/x_train', size(x_train_array), 'Datatype', 'double');
h5write(savefileh5, '/train/x_train', x_train_array);
catch ME
warning('File already in folder or dataset already exists');
end
% Save x_test
try
h5create(savefileh5, '/test/x_test', size(x_test_array), 'Datatype', 'double');
h5write(savefileh5, '/test/x_test', x_test_array);
catch ME
warning('File already in folder or dataset already exists');
end
% Read back the data to verify
x_train_read = h5read(savefileh5, '/train/x_train');
x_test_read = h5read(savefileh5, '/test/x_test');
disp(size(x_train_read));
disp(size(x_test_read));
Please find attached the documentations of “cat” and “h5create” functions in MATLAB for reference:
I believe this will help to resolve the issue.