shared-snippets/matlab/export/gdf2csv.m

143 lines
4.0 KiB
Matlab

% -----------------
% Copyright © 2020 ACK Cyfronet AGH, Poland.
%
% This work was partially funded by EPOS Project funded in frame of PL-POIR4.2
% -----------------
function csvFiles = gdf2csv(gdfFilePath)
csvFiles = {};
load(gdfFilePath);
[~, resultFileNameBase] = fileparts(gdfFilePath);
fieldNames = fieldnames(d);
fieldTypes = getFieldTypes(FieldType, fieldNames);
if (hasSingleData(d, fieldNames))
if (length(d) > 1)
M = prepareCellMatrixFromStructArrayWithScalars(d, fieldTypes);
else
M = prepareCellMatrixFromSingleStructWithVectors(d, fieldTypes, fieldNames);
end
resultFileName = [resultFileNameBase, '.csv'];
saveCsvFile(M, fieldTypes, fieldNames, resultFileName);
csvFiles = { resultFileName };
else
for i = 1:length(d)
resultFileName = [resultFileNameBase, '-', num2str(i), '.csv'];
M = prepareCellMatrixFromSingleStructWithVectors(d(i), fieldTypes, fieldNames);
saveCsvFile(M, fieldTypes, fieldNames, resultFileName);
csvFiles{i} = resultFileName;
end
end
end
function isSingle = hasSingleData(d, fieldNames)
isSingle = length(d) == 1 || ~hasAnyVectors(d, fieldNames);
end
function hasVector = hasAnyVectors(d, fieldNames)
hasVector = false;
for i = 1:length(d)
vectorValue = findFirstVectorValue(d(i), fieldNames);
if ~isempty(vectorValue)
hasVector = true;
return;
end
end
end
function vectorValue = findFirstVectorValue(d, fieldNames)
vectorValue = [];
for i = 1:length(fieldNames)
value = d.(fieldNames{i});
if ~ischar(value) && ~isscalar(value)
vectorValue = value;
return;
end
end
end
function saveCsvFile(M, fieldTypes, fieldNames, filename)
lineFormat = getLineFormat(fieldTypes);
fid = fopen(filename, 'w+');
fprintf(fid, '%s\n', strjoin(fieldNames', ','));
for k=1:size(M,1)
fprintf(fid, lineFormat, M{k, :});
end
fclose(fid);
end
function M = prepareCellMatrixFromSingleStructWithVectors(d, fieldTypes, fieldNames)
% some structures might contain vectors mixed with scalars, in that case we want to repeat the scalar values in the csv file
d = convertScalarsToVectors(d, fieldNames);
M = cell(length(d.(fieldNames{1})), length(fieldNames));
for f = 1:length(fieldTypes)
fieldType = fieldTypes{f};
field = d.(fieldNames{f});
if (isTime(fieldType))
M(:, f) = formatTime(field, fieldType);
elseif isnumeric(field)
if (isrow(field))
field = field';
end
M(:, f) = num2cell(field);
else
if (isrow(field))
field = field';
end
M(:, f) = field;
end
end
end
function M = prepareCellMatrixFromStructArrayWithScalars(d, fieldTypes)
M = squeeze(struct2cell(d))';
M = cellfun(@handleEmptyArray, M, 'UniformOutput', false);
for f = 1:length(fieldTypes)
fieldType = fieldTypes{f};
if (isTime(fieldType))
M(:, f) = formatTime(M(:, f), fieldType);
end
end
end
function struct = convertScalarsToVectors(d, fieldNames)
struct = d;
firstVectorValue = findFirstVectorValue(d, fieldNames);
if isempty(firstVectorValue)
return;
end
count = length(firstVectorValue);
for i = 1:length(fieldNames)
field = struct.(fieldNames{i});
if (isempty(field))
field = nan;
end
if ischar(field) || isscalar(field)
[vectorValue{1:count}] = deal(field);
struct.(fieldNames{i}) = vectorValue';
end
end
end
function value = handleEmptyArray(value)
if (isempty(value))
value = nan;
end
end
% creates a vector of field types written as in FieldType, but preserving the same order of fields as in the 'd'
% structure (fallback for a situation when the types in FieldType are in different order than fieldnames(d) or when
% FieldType contains more entries than fieldnames(d)
function fieldTypes = getFieldTypes(fieldTypeCell, fieldNames)
fieldTypes = cell(1, length(fieldNames));
for i = 1:length(fieldNames)
for j = 1:size(fieldTypeCell, 1)
if strcmp(fieldNames{i}, fieldTypeCell{j, 1})
fieldTypes{i} = fieldTypeCell{j, 2};
end
end
end
end