Made some modifications to preserve original ordering of the table's columns/vars.

This commit is contained in:
florez_j committed 2023-08-15 13:18:40 +02:00
1 parent 5c3679da42
commit 384e5e62d5
1 file changed
+31 -7
+31 -7
View File
@@ -1,4 +1,4 @@
function output_table = load_hdf5_as_table(filename,exclude_var_set)
function output_table = read_hdf5_as_table(filename,exclude_var_set)
% receives a h5 file written with function table2h5.
% reduced_var_set (cell of character_vectors), not usable yet.
@@ -59,18 +59,40 @@ end
% Construct cell array with table data, where each row denotes a group, and
% the columns are either datasets or attributes in the given group.
tmp = {};
idx = [];
group_columns = {};
for i = 1 : num_rows
disp(['Begin construction of row:',INFO.Groups(i).Name])
for j = 1 : num_cols
if j <= num_attr1
ds_name = [INFO.Groups(i).Name '/' INFO.Groups(i).Datasets(j).Name];
%disp(ds_name)
tmp{i,j} = h5read(filename,ds_name);
if i < 2
column_idx = str2double(INFO.Groups(i).Datasets(j).Name(3:end));
idx = [idx,column_idx];
end
end
if j > num_attr2
disp(INFO.Groups(i).Attributes(j-num_attr2).Name)
tmp{i,j} = h5readatt(filename,INFO.Groups(i).Name,INFO.Groups(i).Attributes(j-num_attr2).Name);
if j > num_attr1
Name = INFO.Groups(i).Attributes(j-num_attr1).Name;
tmp{i,j} = h5readatt(filename,INFO.Groups(i).Name, Name);
if i < 2
disp(Name)
% Split attribute name into column_tag (containing original
% column idx) and column_name
delimiter_idx = find(INFO.Groups(i).Attributes(j-num_attr1).Name=='_');
% Left most '_' determines the split location
delimiter_idx = delimiter_idx(1);
column_idx = str2double(Name(5:delimiter_idx-1));
group_columns = [group_columns,{Name(delimiter_idx+1:end)}];
%disp([Name(5:delimiter_idx-1),'-',column_name])
idx = [idx,column_idx];
end
end
@@ -78,8 +100,10 @@ for i = 1 : num_rows
disp(['Ends construction of row:',INFO.Groups(i).Name])
end
% TODO: how can we keep the original ordering of the columns if required by
% Thorsten.
% Find original ordering of columns by sorting idx in ascending order
[~, idx_perm] = sort(idx,'ascend');
varNames = [dataset_columns,group_columns];
output_table = array2table(tmp, 'VariableNames', varNames);
output_table = array2table(tmp(:,idx_perm), 'VariableNames', varNames(idx_perm));