shared-snippets/matlab/catalog/extractColumns.m

54 lines
1.8 KiB
Mathematica
Raw Normal View History

2024-08-28 17:22:21 +02:00
%
% -----------------
% Copyright © 2019 ACK Cyfronet AGH, Poland.
%
% This work was partially funded by EPOS Project funded in frame of PL-POIR4.2
% --------------
%
function [varargout] = extractColumns(catalog, varargin)
2024-08-29 13:11:12 +02:00
% EXTRACTCOLUMNS Extracts specified columns from a catalog structure
%
% [col1, col2, ...] = EXTRACTCOLUMNS(catalog, 'colName1', 'colName2', ...)
% extracts the columns with the specified names from the catalog
% structure. The catalog is expected to be an array of structures where
% each structure has fields 'field' and 'val'. The function returns the
% values of the requested columns.
%
% For more details on the catalog format, see:
% https://docs.cyfronet.pl/display/ISDOC/Catalog+-+description
%
2024-08-29 13:11:12 +02:00
% Example:
% catalog(1).field = 'ID'; catalog(1).val = [1, 2, 3];
% catalog(2).field = 'Time'; catalog(2).val = [100, 200, 300];
% [id, time] = extractColumns(catalog, 'ID', 'Time');
% % Result: id = [1, 2, 3], time = [100, 200, 300]
%
% See also: FINDCATALOGCOLUMN.
for i = 1:length(varargin)
2024-08-28 17:22:21 +02:00
colName = varargin{i};
varargout{i} = findColumn(catalog, colName);
end
end
function column = findColumn(catalog, colName)
2024-08-29 13:11:12 +02:00
% FINDCOLUMN Helper function to find the values of a column in a catalog
%
% column = FINDCOLUMN(catalog, colName) searches the catalog structure
% for the specified column name and returns its values. If the column
% does not exist, an error is thrown.
%
% Refer to the catalog documentation for further information:
% https://docs.cyfronet.pl/display/ISDOC/Catalog+-+description
2024-08-29 13:11:12 +02:00
for c = 1:length(catalog)
2024-08-28 17:22:21 +02:00
if strcmp(catalog(c).field, colName)
column = catalog(c).val;
break;
end
end
if exist('column') ~= 1
error('no column named %s', colName);
end
2024-08-29 13:11:12 +02:00
end