Modified code to write dataset and attributes' names with the indices of the original table's columns. The indices can then inferred by examining the names.

This commit is contained in:
florez_j committed 2023-08-15 13:25:15 +02:00
1 parent 384e5e62d5
commit c23464c795
1 file changed
+53 -14
+53 -14
View File
@@ -16,12 +16,19 @@ else
return
end
% Create array with indices of column_names
column_names_idx = 1 : length(column_names);
% Divide input_table into tables with numeric and cellstr variables/columns
numeric_table = input_table(:,vartype('numeric')); % group attributes of a timestamped measurement
cellstr_table = input_table(:,vartype('cellstr')); % group datasets of a timestamped measurement
% Record columns/vars original index in reference table
numeric_columns = numeric_table.Properties.VariableNames;
numeric_columns_idx = argwhere_var_in_varset(numeric_columns,column_names);
str_columns = cellstr_table.Properties.VariableNames;
str_columns_idx = argwhere_var_in_varset(str_columns,column_names);
% Based on homogeneous columns/cell arrays, we identify the remaining columns
% with possibly heterogeneous entries. Ideally, these should store numeric
@@ -41,6 +48,8 @@ for i = 1 : length(input_table.Properties.VariableNames)
end
end
rest_columns_idx = argwhere_var_in_varset(rest_columns,column_names);
% Create an hdf5 file, where rows are seen as measurements/groups with
% attributes given by numeric and str variables and datasets given by the
% remaining variables as long as they are numeric n-dimensional arrays.
@@ -51,13 +60,14 @@ cell_table = input_table(:,rest_columns);
for i = 1 : rows
group_name = strcat("/MEAS",string(i),"/");
for j = 1 : length(rest_columns)
dataset_name = strcat("DS",string(j));
dataset_name = strcat("DS",string(rest_columns_idx(j)));
% Some table entries do not provide direct access to the object
% of interest, but may contain either {object} or {{object}}.
% The following loop solves that problem. TODO: is this generic enough?
data = cell_table{i,rest_columns{j}}{1};%{1};
data = cell_table{i,column_names{rest_columns_idx(j)}}{1}; % <=> data = cell_table{i,rest_columns{j}}{1};
while iscell(data)
data = data{1};
end
@@ -72,8 +82,13 @@ for i = 1 : rows
h5create(filename, strcat(group_name,dataset_name),sz);
% Dataset's attribute: Add attribute with dataset_name equal to column_name
attr = "column_name";
attr_value = rest_columns{j};
attr_value = column_names{rest_columns_idx(j)}; % <=> rest_columns{j};
h5writeatt(filename,strcat(group_name,dataset_name),attr,attr_value)
%attr = "column_name_idx";
%attr_value = rest_columns_idx(j); % <=> rest_columns{j};
%h5writeatt(filename,strcat(group_name,dataset_name),attr,attr_value)
% Assign 'data' to the dataset. 'data' has to be of admissible type.
try
h5write(filename,strcat(group_name,dataset_name),data)
@@ -84,22 +99,46 @@ for i = 1 : rows
end
% Group's attribute: Assign group attributes
for attr_idx = 1 : max([length(numeric_columns),length(str_columns)])
for attr_idx = 1 : length(numeric_columns)
attr = column_names{numeric_columns_idx(attr_idx)};% <=> attr = numeric_columns{attr_idx};
attr_value = numeric_table{i,attr};
if attr_idx <= length(numeric_columns)
attr = numeric_columns{attr_idx};
attr_value = numeric_table{i,numeric_columns{attr_idx}};
h5writeatt(filename,group_name,attr,attr_value)
end
column_idx_tag = ['ATTR',num2str(numeric_columns_idx(attr_idx)),'_'];
h5writeatt(filename,group_name,[column_idx_tag,attr],attr_value)
if attr_idx <= length(str_columns)
attr = str_columns{attr_idx};
attr_value = cellstr_table{i,str_columns{attr_idx}};
h5writeatt(filename,group_name,attr,attr_value)
end
%attr = "column_name_idx";
%attr_value = numeric_columns_idx(attr_idx); % <=> rest_columns{j};
%h5writeatt(filename,group_name,attr,attr_value)
end
for attr_idx = 1 : length(str_columns)
attr = column_names{str_columns_idx(attr_idx)}; % <=> attr = str_columns{attr_idx}
attr_value = cellstr_table{i,attr};
column_idx_tag = ['ATTR',num2str(str_columns_idx(attr_idx)),'_'];
h5writeatt(filename,group_name,[column_idx_tag,attr],attr_value)
%attr = "column_name_idx";
%attr_value = str_columns_idx(attr_idx); % <=> rest_columns{j};
%h5writeatt(filename,group_name,attr,attr_value)
end
%h5create(filename,"/g2/DS2",[20 Inf],"Chunksize",[5 5]);
end
success = 1;
function idx = argwhere_var_in_varset(var,varset)
ref_idx = 1 : length(varset);
if iscellstr(var)
idx = [];
for i = 1 : length(var)
logical_idx = strcmp(var{i},varset);
idx = [idx,ref_idx(logical_idx)];
end
else
logical_idx = strcmp(var,varset);
idx = ref_idx(logical_idx);
end