shared-snippets/matlab/catalog/sortByTime.m

36 lines
1.2 KiB
Matlab

%
% -----------------
% Copyright © 2022 ACK Cyfronet AGH, Poland.
% -----------------
%
function [sortedCatalog] = sortByTime(catalog)
% SORTBYTIME Sorts a seismic catalog structure by the 'Time' column
%
% sortedCatalog = SORTBYTIME(catalog) sorts the given catalog structure
% based on the values in the 'Time' column. The catalog is expected to be
% an array of structures where each structure has fields 'field' and
% 'val'. The function rearranges the catalog entries in ascending order
% of time.
%
% This function is specific to seismic catalogs. For more general
% information on catalogs, refer to:
% https://docs.cyfronet.pl/display/ISDOC/Catalog+-+description
%
% Example:
% catalog(1).field = 'ID'; catalog(1).val = [3, 2, 1];
% catalog(2).field = 'Time'; catalog(2).val = [300, 200, 100];
% sortedCatalog = sortByTime(catalog);
% % Result: sortedCatalog(2).val = [100, 200, 300]
%
% See also: FINDCATALOGCOLUMN, SORTROWS.
timeColIndex = find(strcmp('Time', {catalog.field}));
if ~isempty(timeColIndex)
[~, sortedIndexes] = sortrows(catalog(timeColIndex).val);
for i = 1:length(catalog)
catalog(i).val = catalog(i).val(sortedIndexes);
end
end
sortedCatalog = catalog;
end