Added two features. 1) to facilitate python access, we now preprocess
input_table so that string variables are changed as cellstr and multicolumn variables (e.g., scientaEkinRange_eV) are splitted into independent variables. 2) we now support when there is no dataset columns (arrays) by creating a dummy dataset called DS_EMPTY. This is required to create a new h5 file for the first time using h5create.
This commit is contained in:
1 parent
ac7f34a30a
commit
bb09c5fe28
1 file changed
+47
-36
+47
-36
@@ -10,6 +10,11 @@ function success = save_table_as_hdf5(input_table, filename)
|
||||
% Email: juan.florez-ospina@psi.ch
|
||||
|
||||
if istable(input_table)
|
||||
|
||||
string_columns = input_table(:,vartype("string")).Properties.VariableNames;
|
||||
input_table = convertvars(input_table,string_columns,'cellstr');
|
||||
% split multicolumn variables
|
||||
input_table = splitvars(input_table);
|
||||
column_names = input_table.Properties.VariableNames;
|
||||
else
|
||||
success = 0;
|
||||
@@ -58,44 +63,50 @@ cell_table = input_table(:,rest_columns);
|
||||
[rows, cols] = size(input_table);
|
||||
|
||||
for i = 1 : rows
|
||||
group_name = strcat("/MEAS",string(i),"/");
|
||||
for j = 1 : length(rest_columns)
|
||||
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,column_names{rest_columns_idx(j)}}{1}; % <=> data = cell_table{i,rest_columns{j}}{1};
|
||||
|
||||
while iscell(data)
|
||||
data = data{1};
|
||||
group_name = strcat("/MEAS_",string(i),"/");
|
||||
if ~isempty(rest_columns)
|
||||
for j = 1 : length(rest_columns)
|
||||
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,column_names{rest_columns_idx(j)}}{1}; % <=> data = cell_table{i,rest_columns{j}}{1};
|
||||
|
||||
while iscell(data)
|
||||
data = data{1};
|
||||
end
|
||||
% when there is no data, we still need to provide a valid size
|
||||
if ~isempty(data)
|
||||
sz = size(data);
|
||||
else
|
||||
sz = [1,1];
|
||||
end
|
||||
%%%%
|
||||
% Group's dataset: Create dataset of size sz in a certain group
|
||||
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 = 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)
|
||||
catch exception
|
||||
disp(exception)
|
||||
%h5write(filename,strcat(group_name,dataset_name),cell_table{i,rest_columns{j}})
|
||||
end
|
||||
end
|
||||
% when there is no data, we still need to provide a valid size
|
||||
if ~isempty(data)
|
||||
sz = size(data);
|
||||
else
|
||||
sz = [1,1];
|
||||
end
|
||||
%%%%
|
||||
% Group's dataset: Create dataset of size sz in a certain group
|
||||
else
|
||||
dataset_name = "DS_EMPTY";
|
||||
sz = [1,1];
|
||||
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 = 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)
|
||||
catch exception
|
||||
disp(exception)
|
||||
%h5write(filename,strcat(group_name,dataset_name),cell_table{i,rest_columns{j}})
|
||||
end
|
||||
end
|
||||
|
||||
% Group's attribute: Assign group attributes
|
||||
|
||||
Reference in new issue
Block a user