Home > database > ensemble_sort.m

ensemble_sort

PURPOSE ^

Sorts an Ensemble datastruct

SYNOPSIS ^

function sorted_st = ensemble_sort(data_st, sort_var, sort_dir)

DESCRIPTION ^

 Sorts an Ensemble datastruct 

 sorted_st = ensemble_sort(data_st, sort_var, sort_dir)

 INPUT:
   data_st: the Ensemble data structure to be sorted
   sort_var: the name of the variable to be sorted by
   sort_dir: the direction ('ascend','descend') to be sorted by. Default
   is 'ascend'

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function sorted_st = ensemble_sort(data_st, sort_var, sort_dir)
0002 % Sorts an Ensemble datastruct
0003 %
0004 % sorted_st = ensemble_sort(data_st, sort_var, sort_dir)
0005 %
0006 % INPUT:
0007 %   data_st: the Ensemble data structure to be sorted
0008 %   sort_var: the name of the variable to be sorted by
0009 %   sort_dir: the direction ('ascend','descend') to be sorted by. Default
0010 %   is 'ascend'
0011 
0012 % 26Jun2014 - Petr Janata
0013 % 09Feb2015 - PJ fixed handling for cells
0014 
0015 % Check to make sure that the variable we want to sort by exists
0016 if ~strcmp(sort_var,data_st.vars)
0017   error('Desired variable for sort (%s) not present in data struct', sort_var)
0018 end
0019 
0020 cols = set_var_col_const(data_st.vars);
0021 
0022 if ~exist('sort_dir','var') || isempty(sort_dir)
0023   sort_dir = 'ascend';
0024 end
0025 
0026 % Copy the data_st to sorted_st
0027 sorted_st = data_st;
0028 sorted_st.data = cell(size(sorted_st.data)); % Zero out the data portion
0029 
0030 % Get the sorted indices
0031 if iscell(data_st.data{cols.(sort_var)})
0032   [~,sort_idxs] = sort(data_st.data{cols.(sort_var)});
0033 else
0034   [~,sort_idxs] = sort(data_st.data{cols.(sort_var)},sort_dir);
0035 end
0036 
0037 % Loop over all the variables and apply the sort
0038 nvar = length(data_st.vars);
0039 for ivar = 1:nvar
0040   sorted_st.data{ivar} = data_st.data{ivar}(sort_idxs);
0041 end
0042 
0043 return

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003