spm_length.m 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. function [n] = spm_length(X)
  2. % Length of a vectorised numeric, cell or structure array
  3. % FORMAT [n] = spm_length(X)
  4. % X - numeric, cell or stucture array[s]
  5. % n - length(spm_vec(X))
  6. %
  7. % See spm_vec, spm_unvec
  8. %__________________________________________________________________________
  9. %
  10. % e.g.:
  11. % spm_length({eye(2) 3}) = 5
  12. %__________________________________________________________________________
  13. % Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging
  14. % Karl Friston
  15. % $Id: spm_length.m 6233 2014-10-12 09:43:50Z karl $
  16. % vectorise numerical arrays
  17. %--------------------------------------------------------------------------
  18. if isnumeric(X)
  19. n = numel(X);
  20. % vectorise logical arrays
  21. %--------------------------------------------------------------------------
  22. elseif islogical(X)
  23. n = numel(X);
  24. % vectorise structure into cell arrays
  25. %--------------------------------------------------------------------------
  26. elseif isstruct(X)
  27. n = 0;
  28. f = fieldnames(X);
  29. for i = 1:numel(f)
  30. for j = 1:numel(X)
  31. n = n + spm_length(X(j).(f{i}));
  32. end
  33. end
  34. % vectorise cells into numerical arrays
  35. %--------------------------------------------------------------------------
  36. elseif iscell(X)
  37. n = 0;
  38. for i = 1:numel(X)
  39. n = n + spm_length(X{i});
  40. end
  41. else
  42. n = 0;
  43. end