getset.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function res = getset(this, parent, fieldname, ind, values)
  2. % Generic method for getting and setting multiple fields of meeg struct
  3. % FORMAT res = getset(this, parent, fieldname, ind, values)
  4. % _______________________________________________________________________
  5. % Copyright (C) 2008-2012 Wellcome Trust Centre for Neuroimaging
  6. % Vladimir Litvak
  7. % $Id: getset.m 6535 2015-08-25 11:45:26Z vladimir $
  8. this = struct(this);
  9. if nargin == 3 || ~isnumeric(ind)
  10. try
  11. ind = 1:numel(getfield(this, parent));
  12. catch
  13. res = [];
  14. return;
  15. end
  16. end
  17. % Get
  18. if nargin <= 4
  19. res = cell(1, length(ind));
  20. for i = 1:length(ind)
  21. res{i} = getfield(this, parent, {ind(i)}, fieldname);
  22. end
  23. if isempty(res) || (all(cellfun('isclass', res, 'double') & (cellfun(@numel, res) == 1)))
  24. res = [res{:}];
  25. end
  26. if iscell(res) && (numel(res) == 1) && (numel(getfield(this, parent)) == 1) &&...
  27. strcmp(parent, 'trials') && strcmp(this.type, 'continuous')
  28. res = res{1};
  29. end
  30. return
  31. end
  32. % Set
  33. if nargin == 5
  34. % This might fail in some pathological cases, but not in what it's
  35. % supposed to be used for.
  36. if (isnumeric(values) || islogical(values)) && (length(values) == length(ind))
  37. values = num2cell(values);
  38. end
  39. if iscell(values) && ~(numel(values)==1 || numel(values) == length(ind))
  40. error('Illegal assignment: cannot match values and indices.');
  41. end
  42. for i = 1:length(ind)
  43. if iscell(values)
  44. this = setfield(this, parent, {ind(i)}, fieldname, values{i});
  45. else
  46. this = setfield(this, parent, {ind(i)}, fieldname, values);
  47. end
  48. end
  49. % getset is sometimes used on subfields of meeg then checkmeeg should
  50. % not be used
  51. if all(isfield(this, {'type', 'Nsamples', 'Fsample', 'timeOnset'}))
  52. res = meeg(this);
  53. else
  54. res = this;
  55. end
  56. end