forked from episodes-platform/shared-snippets
56 lines
1.3 KiB
Mathematica
56 lines
1.3 KiB
Mathematica
|
% -----------------
|
||
|
% Copyright © 2022 ACK Cyfronet AGH, Poland.
|
||
|
% -----------------
|
||
|
function catalog2csv(catalog, csvFileName)
|
||
|
|
||
|
fieldNames = getFieldNames(catalog);
|
||
|
fieldTypes = getFieldTypes(catalog);
|
||
|
lineFormat = getLineFormat(fieldTypes);
|
||
|
M = prepareCellMatrix(catalog);
|
||
|
|
||
|
fid = fopen(csvFileName, 'w+');
|
||
|
fprintf(fid, '%s\n', strjoin(fieldNames', ','));
|
||
|
for k=1:size(M,1)
|
||
|
fprintf(fid, lineFormat, M{k, :});
|
||
|
end
|
||
|
fclose(fid);
|
||
|
|
||
|
end
|
||
|
|
||
|
function fieldNames = getFieldNames(catalog)
|
||
|
fieldNames = [];
|
||
|
for i=1:length(catalog)
|
||
|
fieldNames{i} = catalog(i).field;
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function fieldTypes = getFieldTypes(catalog)
|
||
|
fieldTypes = [];
|
||
|
for i=1:length(catalog)
|
||
|
fieldTypes{i} = catalog(i).type;
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function M = prepareCellMatrix(catalog)
|
||
|
M = {};
|
||
|
for i=1:length(catalog)
|
||
|
val = catalog(i).val;
|
||
|
type = catalog(i).type;
|
||
|
if iscell(val)
|
||
|
M(:,i) = val;
|
||
|
elseif isTime(type)
|
||
|
M(:,i) = formatCatalogTime(val, type);
|
||
|
else
|
||
|
M(:,i) = num2cell(val);
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function timeStrVector = formatCatalogTime(timeVector, fieldType)
|
||
|
if iscell(timeVector)
|
||
|
timeVector = cell2mat(timeVector);
|
||
|
end
|
||
|
emptyIndexes = isnan(timeVector);
|
||
|
timeStrVector(~emptyIndexes, 1) = cellstr(datestr(timeVector(~emptyIndexes), 'yyyy-mm-dd HH:MM:SS.FFF'));
|
||
|
timeStrVector(emptyIndexes, 1) = { 'NaN' };
|
||
|
end
|