subsasgn.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. function this = subsasgn(this, subs, A)
  2. % Subscript assignment for GIfTI objects
  3. %__________________________________________________________________________
  4. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  5. % Guillaume Flandin
  6. % $Id: subsasgn.m 6513 2015-08-05 17:52:13Z guillaume $
  7. switch subs(1).type
  8. case '.'
  9. if ~ismember(subs(1).subs, ...
  10. {'vertices' 'faces' 'normals' 'cdata','mat','indices','private'})
  11. error('Reference to non-existent field ''%s''.',subs(1).subs);
  12. end
  13. % TODO % handle cases when length(subs) > 1
  14. [i,n] = isintent(this,subs(1).subs);
  15. if isempty(i) && ~strcmp(subs(1).subs,'private')
  16. n = length(this.data) + 1;
  17. if n==1, this.data = {}; end
  18. % TODO % Initialise data field appropriately
  19. this.data{n}.metadata = struct([]);
  20. this.data{n}.space = [];
  21. this.data{n}.attributes.Dim = size(A);
  22. % TODO % set DataType according to intent type
  23. this.data{n}.data = [];
  24. switch subs(1).subs
  25. case {'vertices','mat'}
  26. in = 'NIFTI_INTENT_POINTSET';
  27. dt = 'NIFTI_TYPE_FLOAT32';
  28. this.data{n}.space.DataSpace = 'NIFTI_XFORM_UNKNOWN';
  29. this.data{n}.space.TransformedSpace = 'NIFTI_XFORM_UNKNOWN';
  30. this.data{n}.space.MatrixData = eye(4);
  31. case 'faces'
  32. in = 'NIFTI_INTENT_TRIANGLE';
  33. dt = 'NIFTI_TYPE_INT32';
  34. case 'indices'
  35. in = 'NIFTI_INTENT_NODE_INDEX';
  36. dt = 'NIFTI_TYPE_INT32';
  37. case 'normals'
  38. in = 'NIFTI_INTENT_VECTOR';
  39. dt = 'NIFTI_TYPE_FLOAT32';
  40. case 'cdata'
  41. in = 'NIFTI_INTENT_NONE';
  42. dt = 'NIFTI_TYPE_FLOAT32';
  43. otherwise
  44. error('This should not happen.');
  45. end
  46. this.data{n}.attributes.Intent = in;
  47. this.data{n}.attributes.DataType = dt;
  48. end
  49. switch subs(1).subs
  50. %- .private
  51. %--------------------------------------------------------------
  52. case 'private'
  53. this = builtin('subsasgn',this,subs(2:end),A);
  54. % .mat
  55. %--------------------------------------------------------------
  56. case 'mat'
  57. if length(subs) > 1
  58. this.data{n}.space(1).MatrixData = builtin('subsasgn',...
  59. this.data{n}.space(1).MatrixData,subs(2:end),A);
  60. else
  61. if ~isequal(size(A),[4 4])
  62. error('Invalid Coordinate System Transform Matrix.');
  63. end
  64. this.data{n}.space(1).MatrixData = A;
  65. end
  66. %- .faces
  67. %--------------------------------------------------------------
  68. case 'faces'
  69. if length(subs) > 1
  70. this.data{n}.data = int32(builtin('subsasgn',this.data{n}.data,subs(2:end),A-1));
  71. else
  72. this.data{n}.data = int32(A - 1);
  73. this.data{n}.attributes.Dim = size(A);
  74. end
  75. %- .indices
  76. %--------------------------------------------------------------
  77. case 'indices'
  78. if n ~= 1
  79. this.data = this.data([n setdiff(1:numel(this.data),n)]);
  80. n = 1;
  81. end
  82. if length(subs) > 1
  83. this.data{n}.data = int32(builtin('subsasgn',this.data{n}.data,subs(2:end),A-1));
  84. else
  85. A = A(:);
  86. this.data{n}.data = int32(A - 1);
  87. this.data{n}.attributes.Dim = size(A);
  88. end
  89. %- .vertices, .normals, .cdata
  90. %--------------------------------------------------------------
  91. otherwise
  92. if length(subs) > 1
  93. if numel(n) == 1
  94. this.data{n}.data = single(builtin('subsasgn',this.data{n}.data,subs(2:end),A));
  95. this.data{n}.attributes.Dim = size(this.data{n}.data);
  96. else
  97. if numel(subs(2).subs) == 1
  98. error('Linear indexing not supported: use multiple subscripts.');
  99. end
  100. idx = subs(2).subs{2};
  101. if isequal(idx,':'), idx = 1:numel(this.data); end
  102. for k=1:numel(idx)
  103. s = subs(2);
  104. s.subs{2} = 1;
  105. if numel(A) == 1
  106. this.data{idx(k)}.data = single(builtin('subsasgn',this.data{idx(k)}.data,s,A));
  107. else
  108. this.data{idx(k)}.data = single(builtin('subsasgn',this.data{idx(k)}.data,s,A(:,k)));
  109. end
  110. this.data{idx(k)}.attributes.Dim = size(this.data{idx(k)}.data);
  111. end
  112. end
  113. else
  114. if numel(n) == 1
  115. if isa(A,'file_array')
  116. this.data{n}.data = A;
  117. this.data{n}.attributes.Dim = A.dim;
  118. else
  119. this.data{n}.data = single(A);
  120. this.data{n}.attributes.Dim = size(A);
  121. end
  122. else
  123. error('Syntax not implemented.');
  124. end
  125. end
  126. end
  127. case '()'
  128. case '{}'
  129. otherwise
  130. error('This should not happen.');
  131. end