How to write struct back to h5 file

24 views (last 30 days)
I am using a program called DeepLabCut to do markerless motion capture. After manually labelling data on still images, the program creates an h5 file (downloadable at this Google Drive link because Matlab Answers does not support uploading h5 files), part of which contains some metadata about the videos and the current project. I need to change that metadata in the h5 file, but am stuck on how to write that data back to the h5 file because it seems to be a struct.
I have also attached a csv file which is also generated as the human-readable version of this data, although the program only uses the h5 file.
First, I read in the h5 file and define the new metadata to be written.
% The full path to the h5 file
h5FilePath='/Volumes/GoogleDrive-117132924848468950297/My Drive/NJ GSS/DLC Home/NJ GSS Live-Group 8-2021-11-25/Folders Needing Renaming/standingBackOutsideShoes1/CollectedData_Group 8.h5';
% Read the h5 file and isolate the relevant metadata (don't need to modify
% the other field in the h5table struct).
h5table=h5read(h5FilePath,'/df_with_missing/table');
index=h5table.index;
% The first column of the csv (and the index field of h5table) contains a relative path. This is the prefix value that it
% should be changed to.
newPrefix='labeled-data/standingBackOutsideShoes1/';
Then, I modify that metadata.
% Change the value of index, where each column of the char array is one
% full path (prefix + img number)
clear newIndex;
for i=1:size(index,2)
if i<10
strNum=['00' num2str(i)];
elseif i<100
strNum=['0' num2str(i)];
else
strNum=num2str(i);
end
newIndex(:,i)=[newPrefix 'img' strNum '.png'];
end
h5table.index=newIndex;
Now, the h5table.index field has been appropriately modified. But I cannot write that same data back to the h5 file. The below code returns an error saying that type struct is not allowed, even though that is what was just read from the h5 file!
h5write(h5FilePath,'/df_with_missing/table',h5table);
Am I missing something? I'm sure this has something to do with type conversions between Matlab and h5, but I can't figure it out. How can I write back both fields of the h5table structure to that dataset in the h5 file? Thanks in advance.

Accepted Answer

Voss
Voss on 23 Jan 2022
This is a partial answer, at best. But here's how you can modify the 'index' member of the 'table' dataset in that .h5 file, under the condition that the size of 'index' does not change. This is using code I found in the File Exchange: Edit_struct_hdf5 by Renato Medeiros
Since the size of table.index cannot change, I'll use newPrefix='labeled-data/standingBackOutsideShoes001/'; where I added two 0's to keep the size of index the same as it was in the file originally. Again, that may or may not work for what you need to do, but it's the best I could figure out and maybe you can improve upon it.
[Also, note that newIndex in your code creates file names numbered 1 - 138 whereas the original index had 0 - 137, so I'm using 0 - 137 to be consistent with the original (and using sprintf('...%03d...',...) to avoid having to check how many zeros need to be prepended on each number).]
[I changed the extension from .h5 to .mat to get it to upload here and then copy the file to .h5 at the top of the code.]
copyfile('CollectedData_Group 8.mat','CollectedData_Group 8.h5');
h5FilePath = 'CollectedData_Group 8.h5';
h5DataSet = '/df_with_missing/table';
% show the initial contents of table.index:
h5table = h5read(h5FilePath,h5DataSet);
display(h5table.index.');
labeled-data/standing_backOutsidew:shoes/img000.png labeled-data/standing_backOutsidew:shoes/img001.png labeled-data/standing_backOutsidew:shoes/img002.png labeled-data/standing_backOutsidew:shoes/img003.png labeled-data/standing_backOutsidew:shoes/img004.png labeled-data/standing_backOutsidew:shoes/img005.png labeled-data/standing_backOutsidew:shoes/img006.png labeled-data/standing_backOutsidew:shoes/img007.png labeled-data/standing_backOutsidew:shoes/img008.png labeled-data/standing_backOutsidew:shoes/img009.png labeled-data/standing_backOutsidew:shoes/img010.png labeled-data/standing_backOutsidew:shoes/img011.png labeled-data/standing_backOutsidew:shoes/img012.png labeled-data/standing_backOutsidew:shoes/img013.png labeled-data/standing_backOutsidew:shoes/img014.png labeled-data/standing_backOutsidew:shoes/img015.png labeled-data/standing_backOutsidew:shoes/img016.png labeled-data/standing_backOutsidew:shoes/img017.png labeled-data/standing_backOutsidew:shoes/img018.png labeled-data/standing_backOutsidew:shoes/img019.png labeled-data/standing_backOutsidew:shoes/img020.png labeled-data/standing_backOutsidew:shoes/img021.png labeled-data/standing_backOutsidew:shoes/img022.png labeled-data/standing_backOutsidew:shoes/img023.png labeled-data/standing_backOutsidew:shoes/img024.png labeled-data/standing_backOutsidew:shoes/img025.png labeled-data/standing_backOutsidew:shoes/img026.png labeled-data/standing_backOutsidew:shoes/img027.png labeled-data/standing_backOutsidew:shoes/img028.png labeled-data/standing_backOutsidew:shoes/img029.png labeled-data/standing_backOutsidew:shoes/img030.png labeled-data/standing_backOutsidew:shoes/img031.png labeled-data/standing_backOutsidew:shoes/img032.png labeled-data/standing_backOutsidew:shoes/img033.png labeled-data/standing_backOutsidew:shoes/img034.png labeled-data/standing_backOutsidew:shoes/img035.png labeled-data/standing_backOutsidew:shoes/img036.png labeled-data/standing_backOutsidew:shoes/img037.png labeled-data/standing_backOutsidew:shoes/img038.png labeled-data/standing_backOutsidew:shoes/img039.png labeled-data/standing_backOutsidew:shoes/img040.png labeled-data/standing_backOutsidew:shoes/img041.png labeled-data/standing_backOutsidew:shoes/img042.png labeled-data/standing_backOutsidew:shoes/img043.png labeled-data/standing_backOutsidew:shoes/img044.png labeled-data/standing_backOutsidew:shoes/img045.png labeled-data/standing_backOutsidew:shoes/img046.png labeled-data/standing_backOutsidew:shoes/img047.png labeled-data/standing_backOutsidew:shoes/img048.png labeled-data/standing_backOutsidew:shoes/img049.png labeled-data/standing_backOutsidew:shoes/img050.png labeled-data/standing_backOutsidew:shoes/img051.png labeled-data/standing_backOutsidew:shoes/img052.png labeled-data/standing_backOutsidew:shoes/img053.png labeled-data/standing_backOutsidew:shoes/img054.png labeled-data/standing_backOutsidew:shoes/img055.png labeled-data/standing_backOutsidew:shoes/img056.png labeled-data/standing_backOutsidew:shoes/img057.png labeled-data/standing_backOutsidew:shoes/img058.png labeled-data/standing_backOutsidew:shoes/img059.png labeled-data/standing_backOutsidew:shoes/img060.png labeled-data/standing_backOutsidew:shoes/img061.png labeled-data/standing_backOutsidew:shoes/img062.png labeled-data/standing_backOutsidew:shoes/img063.png labeled-data/standing_backOutsidew:shoes/img064.png labeled-data/standing_backOutsidew:shoes/img065.png labeled-data/standing_backOutsidew:shoes/img066.png labeled-data/standing_backOutsidew:shoes/img067.png labeled-data/standing_backOutsidew:shoes/img068.png labeled-data/standing_backOutsidew:shoes/img069.png labeled-data/standing_backOutsidew:shoes/img070.png labeled-data/standing_backOutsidew:shoes/img071.png labeled-data/standing_backOutsidew:shoes/img072.png labeled-data/standing_backOutsidew:shoes/img073.png labeled-data/standing_backOutsidew:shoes/img074.png labeled-data/standing_backOutsidew:shoes/img075.png labeled-data/standing_backOutsidew:shoes/img076.png labeled-data/standing_backOutsidew:shoes/img077.png labeled-data/standing_backOutsidew:shoes/img078.png labeled-data/standing_backOutsidew:shoes/img079.png labeled-data/standing_backOutsidew:shoes/img080.png labeled-data/standing_backOutsidew:shoes/img081.png labeled-data/standing_backOutsidew:shoes/img082.png labeled-data/standing_backOutsidew:shoes/img083.png labeled-data/standing_backOutsidew:shoes/img084.png labeled-data/standing_backOutsidew:shoes/img085.png labeled-data/standing_backOutsidew:shoes/img086.png labeled-data/standing_backOutsidew:shoes/img087.png labeled-data/standing_backOutsidew:shoes/img088.png labeled-data/standing_backOutsidew:shoes/img089.png labeled-data/standing_backOutsidew:shoes/img090.png labeled-data/standing_backOutsidew:shoes/img091.png labeled-data/standing_backOutsidew:shoes/img092.png labeled-data/standing_backOutsidew:shoes/img093.png labeled-data/standing_backOutsidew:shoes/img094.png labeled-data/standing_backOutsidew:shoes/img095.png labeled-data/standing_backOutsidew:shoes/img096.png labeled-data/standing_backOutsidew:shoes/img097.png labeled-data/standing_backOutsidew:shoes/img098.png labeled-data/standing_backOutsidew:shoes/img099.png labeled-data/standing_backOutsidew:shoes/img100.png labeled-data/standing_backOutsidew:shoes/img101.png labeled-data/standing_backOutsidew:shoes/img102.png labeled-data/standing_backOutsidew:shoes/img103.png labeled-data/standing_backOutsidew:shoes/img104.png labeled-data/standing_backOutsidew:shoes/img105.png labeled-data/standing_backOutsidew:shoes/img106.png labeled-data/standing_backOutsidew:shoes/img107.png labeled-data/standing_backOutsidew:shoes/img108.png labeled-data/standing_backOutsidew:shoes/img109.png labeled-data/standing_backOutsidew:shoes/img110.png labeled-data/standing_backOutsidew:shoes/img111.png labeled-data/standing_backOutsidew:shoes/img112.png labeled-data/standing_backOutsidew:shoes/img113.png labeled-data/standing_backOutsidew:shoes/img114.png labeled-data/standing_backOutsidew:shoes/img115.png labeled-data/standing_backOutsidew:shoes/img116.png labeled-data/standing_backOutsidew:shoes/img117.png labeled-data/standing_backOutsidew:shoes/img118.png labeled-data/standing_backOutsidew:shoes/img119.png labeled-data/standing_backOutsidew:shoes/img120.png labeled-data/standing_backOutsidew:shoes/img121.png labeled-data/standing_backOutsidew:shoes/img122.png labeled-data/standing_backOutsidew:shoes/img123.png labeled-data/standing_backOutsidew:shoes/img124.png labeled-data/standing_backOutsidew:shoes/img125.png labeled-data/standing_backOutsidew:shoes/img126.png labeled-data/standing_backOutsidew:shoes/img127.png labeled-data/standing_backOutsidew:shoes/img128.png labeled-data/standing_backOutsidew:shoes/img129.png labeled-data/standing_backOutsidew:shoes/img130.png labeled-data/standing_backOutsidew:shoes/img131.png labeled-data/standing_backOutsidew:shoes/img132.png labeled-data/standing_backOutsidew:shoes/img133.png labeled-data/standing_backOutsidew:shoes/img134.png labeled-data/standing_backOutsidew:shoes/img135.png labeled-data/standing_backOutsidew:shoes/img136.png labeled-data/standing_backOutsidew:shoes/img137.png
% build the new table.index:
% newPrefix = 'labeled-data/standingBackOutsideShoes1/';
newPrefix = 'labeled-data/standingBackOutsideShoes001/';
newIndex = '';
for i = 1:size(h5table.index,2)
newIndex(:,i) = sprintf('%simg%03d.png',newPrefix,i-1);
end
h5table.index = newIndex;
% modify the file:
fileattrib(h5FilePath,'+w');
plist = 'H5P_DEFAULT';
fid = H5F.open(h5FilePath,'H5F_ACC_RDWR',plist);
dset_id = H5D.open(fid,h5DataSet);
H5D.write(dset_id,'H5ML_DEFAULT','H5S_ALL','H5S_ALL',plist,h5table);
H5D.close(dset_id);
H5F.close(fid);
% check the file now:
h5table = h5read(h5FilePath,h5DataSet);
display(h5table.index.');
labeled-data/standingBackOutsideShoes001/img000.png labeled-data/standingBackOutsideShoes001/img001.png labeled-data/standingBackOutsideShoes001/img002.png labeled-data/standingBackOutsideShoes001/img003.png labeled-data/standingBackOutsideShoes001/img004.png labeled-data/standingBackOutsideShoes001/img005.png labeled-data/standingBackOutsideShoes001/img006.png labeled-data/standingBackOutsideShoes001/img007.png labeled-data/standingBackOutsideShoes001/img008.png labeled-data/standingBackOutsideShoes001/img009.png labeled-data/standingBackOutsideShoes001/img010.png labeled-data/standingBackOutsideShoes001/img011.png labeled-data/standingBackOutsideShoes001/img012.png labeled-data/standingBackOutsideShoes001/img013.png labeled-data/standingBackOutsideShoes001/img014.png labeled-data/standingBackOutsideShoes001/img015.png labeled-data/standingBackOutsideShoes001/img016.png labeled-data/standingBackOutsideShoes001/img017.png labeled-data/standingBackOutsideShoes001/img018.png labeled-data/standingBackOutsideShoes001/img019.png labeled-data/standingBackOutsideShoes001/img020.png labeled-data/standingBackOutsideShoes001/img021.png labeled-data/standingBackOutsideShoes001/img022.png labeled-data/standingBackOutsideShoes001/img023.png labeled-data/standingBackOutsideShoes001/img024.png labeled-data/standingBackOutsideShoes001/img025.png labeled-data/standingBackOutsideShoes001/img026.png labeled-data/standingBackOutsideShoes001/img027.png labeled-data/standingBackOutsideShoes001/img028.png labeled-data/standingBackOutsideShoes001/img029.png labeled-data/standingBackOutsideShoes001/img030.png labeled-data/standingBackOutsideShoes001/img031.png labeled-data/standingBackOutsideShoes001/img032.png labeled-data/standingBackOutsideShoes001/img033.png labeled-data/standingBackOutsideShoes001/img034.png labeled-data/standingBackOutsideShoes001/img035.png labeled-data/standingBackOutsideShoes001/img036.png labeled-data/standingBackOutsideShoes001/img037.png labeled-data/standingBackOutsideShoes001/img038.png labeled-data/standingBackOutsideShoes001/img039.png labeled-data/standingBackOutsideShoes001/img040.png labeled-data/standingBackOutsideShoes001/img041.png labeled-data/standingBackOutsideShoes001/img042.png labeled-data/standingBackOutsideShoes001/img043.png labeled-data/standingBackOutsideShoes001/img044.png labeled-data/standingBackOutsideShoes001/img045.png labeled-data/standingBackOutsideShoes001/img046.png labeled-data/standingBackOutsideShoes001/img047.png labeled-data/standingBackOutsideShoes001/img048.png labeled-data/standingBackOutsideShoes001/img049.png labeled-data/standingBackOutsideShoes001/img050.png labeled-data/standingBackOutsideShoes001/img051.png labeled-data/standingBackOutsideShoes001/img052.png labeled-data/standingBackOutsideShoes001/img053.png labeled-data/standingBackOutsideShoes001/img054.png labeled-data/standingBackOutsideShoes001/img055.png labeled-data/standingBackOutsideShoes001/img056.png labeled-data/standingBackOutsideShoes001/img057.png labeled-data/standingBackOutsideShoes001/img058.png labeled-data/standingBackOutsideShoes001/img059.png labeled-data/standingBackOutsideShoes001/img060.png labeled-data/standingBackOutsideShoes001/img061.png labeled-data/standingBackOutsideShoes001/img062.png labeled-data/standingBackOutsideShoes001/img063.png labeled-data/standingBackOutsideShoes001/img064.png labeled-data/standingBackOutsideShoes001/img065.png labeled-data/standingBackOutsideShoes001/img066.png labeled-data/standingBackOutsideShoes001/img067.png labeled-data/standingBackOutsideShoes001/img068.png labeled-data/standingBackOutsideShoes001/img069.png labeled-data/standingBackOutsideShoes001/img070.png labeled-data/standingBackOutsideShoes001/img071.png labeled-data/standingBackOutsideShoes001/img072.png labeled-data/standingBackOutsideShoes001/img073.png labeled-data/standingBackOutsideShoes001/img074.png labeled-data/standingBackOutsideShoes001/img075.png labeled-data/standingBackOutsideShoes001/img076.png labeled-data/standingBackOutsideShoes001/img077.png labeled-data/standingBackOutsideShoes001/img078.png labeled-data/standingBackOutsideShoes001/img079.png labeled-data/standingBackOutsideShoes001/img080.png labeled-data/standingBackOutsideShoes001/img081.png labeled-data/standingBackOutsideShoes001/img082.png labeled-data/standingBackOutsideShoes001/img083.png labeled-data/standingBackOutsideShoes001/img084.png labeled-data/standingBackOutsideShoes001/img085.png labeled-data/standingBackOutsideShoes001/img086.png labeled-data/standingBackOutsideShoes001/img087.png labeled-data/standingBackOutsideShoes001/img088.png labeled-data/standingBackOutsideShoes001/img089.png labeled-data/standingBackOutsideShoes001/img090.png labeled-data/standingBackOutsideShoes001/img091.png labeled-data/standingBackOutsideShoes001/img092.png labeled-data/standingBackOutsideShoes001/img093.png labeled-data/standingBackOutsideShoes001/img094.png labeled-data/standingBackOutsideShoes001/img095.png labeled-data/standingBackOutsideShoes001/img096.png labeled-data/standingBackOutsideShoes001/img097.png labeled-data/standingBackOutsideShoes001/img098.png labeled-data/standingBackOutsideShoes001/img099.png labeled-data/standingBackOutsideShoes001/img100.png labeled-data/standingBackOutsideShoes001/img101.png labeled-data/standingBackOutsideShoes001/img102.png labeled-data/standingBackOutsideShoes001/img103.png labeled-data/standingBackOutsideShoes001/img104.png labeled-data/standingBackOutsideShoes001/img105.png labeled-data/standingBackOutsideShoes001/img106.png labeled-data/standingBackOutsideShoes001/img107.png labeled-data/standingBackOutsideShoes001/img108.png labeled-data/standingBackOutsideShoes001/img109.png labeled-data/standingBackOutsideShoes001/img110.png labeled-data/standingBackOutsideShoes001/img111.png labeled-data/standingBackOutsideShoes001/img112.png labeled-data/standingBackOutsideShoes001/img113.png labeled-data/standingBackOutsideShoes001/img114.png labeled-data/standingBackOutsideShoes001/img115.png labeled-data/standingBackOutsideShoes001/img116.png labeled-data/standingBackOutsideShoes001/img117.png labeled-data/standingBackOutsideShoes001/img118.png labeled-data/standingBackOutsideShoes001/img119.png labeled-data/standingBackOutsideShoes001/img120.png labeled-data/standingBackOutsideShoes001/img121.png labeled-data/standingBackOutsideShoes001/img122.png labeled-data/standingBackOutsideShoes001/img123.png labeled-data/standingBackOutsideShoes001/img124.png labeled-data/standingBackOutsideShoes001/img125.png labeled-data/standingBackOutsideShoes001/img126.png labeled-data/standingBackOutsideShoes001/img127.png labeled-data/standingBackOutsideShoes001/img128.png labeled-data/standingBackOutsideShoes001/img129.png labeled-data/standingBackOutsideShoes001/img130.png labeled-data/standingBackOutsideShoes001/img131.png labeled-data/standingBackOutsideShoes001/img132.png labeled-data/standingBackOutsideShoes001/img133.png labeled-data/standingBackOutsideShoes001/img134.png labeled-data/standingBackOutsideShoes001/img135.png labeled-data/standingBackOutsideShoes001/img136.png labeled-data/standingBackOutsideShoes001/img137.png
  1 Comment
Mitchell Tillman
Mitchell Tillman on 24 Jan 2022
This was exactly what I needed, thank you! Especially the bit at the end about how to actually write the data. I couldn't figure out some of the parameters for H5F.open or H5D.write.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!