structindex

PURPOSE ^

SYNOPSIS ^

function s = structindex(s,slength,sindex,ignoredfields)

DESCRIPTION ^

 This function returns the fields evaluated at the index sindex.
  - Only fields with a dimension of length slength are concerned 
  - If several dimensions have the length slength, only the first dimension is concerned

 by J. Decker 02/01/2011

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function s = structindex(s,slength,sindex,ignoredfields)
0002 %
0003 % This function returns the fields evaluated at the index sindex.
0004 %  - Only fields with a dimension of length slength are concerned
0005 %  - If several dimensions have the length slength, only the first dimension is concerned
0006 %
0007 % by J. Decker 02/01/2011
0008 %
0009 if nargin <4,
0010     ignoredfields = {};
0011 end
0012 if nargin < 3,
0013     error('Not enough input arguments')
0014 end
0015 if ~isstruct(s),
0016     error('s must be s structure')
0017 end
0018 if ~isscalar(slength) || slength ~= round(slength) || slength <= 0,
0019     error('slength must be a positive integer scalar')
0020 end
0021 if ~isscalar(sindex) || sindex ~= round(sindex) || sindex <= 0,
0022     error('sindex must be a positive integer scalar')
0023 end
0024 if sindex > slength,
0025     error('sindex cannot be larger than slength')
0026 end
0027 %
0028 sfields = fieldnames(s);
0029 %
0030 for is = 1:length(sfields),
0031     %
0032     sfield = sfields{is}; 
0033     %
0034     if any(strmatch(sfield,ignoredfields,'exact')),
0035         continue
0036     end
0037     %
0038     sval = s.(sfield);
0039     ssize = size(sval);
0040     idim = find(ssize == slength,1,'first');
0041     %
0042     if ~isempty(idim)
0043         ssize2 = ssize([idim:end,1:idim-1]);
0044         sval = shiftdim(sval,idim - 1);
0045         sval = reshape(sval(sindex,:),[1,ssize2(2:end)]);
0046         sval = shiftdim(sval,length(ssize) - idim + 1);
0047         s.(sfield) = sval;
0048     end
0049 end
0050 %

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.