coor2D.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. function [res, plotind] = coor2D(this, ind, val, mindist)
  2. % returns x and y coordinates of channels in 2D plane
  3. % FORMAT coor2D(this)
  4. % _______________________________________________________________________
  5. % Copyright (C) 2008-2012 Wellcome Trust Centre for Neuroimaging
  6. % Vladimir Litvak, Laurence Hunt
  7. % $Id: coor2D.m 7445 2018-10-12 13:24:48Z vladimir $
  8. megind = indchantype(this, {'MEG', 'MEGPLANAR', 'MEGCOMB'});
  9. eegind = indchantype(this, {'EEG'});
  10. otherind = setdiff(1:nchannels(this), [megind eegind]);
  11. if nargin==1 || isempty(ind)
  12. if nargin<3 || (size(val, 2)<nchannels(this))
  13. if ~isempty(megind)
  14. ind = megind;
  15. elseif ~isempty(eegind)
  16. ind = eegind;
  17. else
  18. ind = 1:nchannels(this);
  19. end
  20. else
  21. ind = 1:nchannels(this);
  22. end
  23. elseif ischar(ind)
  24. switch upper(ind)
  25. case 'MEG'
  26. ind = megind;
  27. case 'EEG'
  28. ind = eegind;
  29. case ':'
  30. ind = 1:nchannels(this);
  31. otherwise
  32. ind = otherind;
  33. end
  34. end
  35. if nargin < 3 || isempty(val)
  36. if ~isempty(intersect(ind, megind))
  37. if this.montage.Mind==0
  38. if ~any(cellfun('isempty', {this.channels(megind).X_plot2D this.channels(megind).Y_plot2D}))
  39. meg_xy = [this.channels(megind).X_plot2D; this.channels(megind).Y_plot2D];
  40. elseif all(cellfun('isempty', {this.channels(megind).X_plot2D this.channels(megind).Y_plot2D}))
  41. meg_xy = grid(length(megind));
  42. else
  43. error('Either all or none of MEG channels should have 2D coordinates defined.');
  44. end
  45. else
  46. if ~any(cellfun('isempty', {this.montage.M(this.montage.Mind).channels(megind).X_plot2D ...
  47. this.montage.M(this.montage.Mind).channels(megind).Y_plot2D}))
  48. meg_xy = [this.montage.M(this.montage.Mind).channels(megind).X_plot2D; ...
  49. this.montage.M(this.montage.Mind).channels(megind).Y_plot2D];
  50. elseif all(cellfun('isempty', {this.montage.M(this.montage.Mind).channels(megind).X_plot2D...
  51. this.montage.M(this.montage.Mind).channels(megind).Y_plot2D}))
  52. meg_xy = grid(length(megind));
  53. else
  54. error('Either all or none of EEG channels should have 2D coordinates defined.');
  55. end
  56. end
  57. end
  58. if ~isempty(intersect(ind, eegind))
  59. if this.montage.Mind==0
  60. if ~any(cellfun('isempty', {this.channels(eegind).X_plot2D this.channels(eegind).Y_plot2D}))
  61. eeg_xy = [this.channels(eegind).X_plot2D; this.channels(eegind).Y_plot2D];
  62. elseif all(cellfun('isempty', {this.channels(eegind).X_plot2D this.channels(eegind).Y_plot2D}))
  63. eeg_xy = grid(length(eegind));
  64. else
  65. error('Either all or none of EEG channels should have 2D coordinates defined.');
  66. end
  67. else
  68. if ~any(cellfun('isempty', {this.montage.M(this.montage.Mind).channels(eegind).X_plot2D ...
  69. this.montage.M(this.montage.Mind).channels(eegind).Y_plot2D}))
  70. eeg_xy = [this.montage.M(this.montage.Mind).channels(eegind).X_plot2D; ...
  71. this.montage.M(this.montage.Mind).channels(eegind).Y_plot2D];
  72. elseif all(cellfun('isempty', {this.montage.M(this.montage.Mind).channels(eegind).X_plot2D...
  73. this.montage.M(this.montage.Mind).channels(eegind).Y_plot2D}))
  74. eeg_xy = grid(length(eegind));
  75. else
  76. error('Either all or none of EEG channels should have 2D coordinates defined.');
  77. end
  78. end
  79. end
  80. if ~isempty(intersect(ind, otherind))
  81. other_xy = grid(length(otherind));
  82. end
  83. xy = zeros(2, length(ind));
  84. plotind = zeros(1, length(ind));
  85. for i = 1:length(ind)
  86. [found, loc] = ismember(ind(i), megind);
  87. if found
  88. xy(:, i) = meg_xy(:, loc);
  89. plotind(i) = 1;
  90. else
  91. [found, loc] = ismember(ind(i), eegind);
  92. if found
  93. xy(:, i) = eeg_xy(:, loc);
  94. plotind(i) = 2;
  95. else
  96. [found, loc] = ismember(ind(i), otherind);
  97. if found
  98. xy(:, i) = other_xy(:, loc);
  99. plotind(i) = 3;
  100. end
  101. end
  102. end
  103. end
  104. if nargin > 3 && ~isempty(mindist)
  105. xy = shiftxy(xy,mindist);
  106. end
  107. res = xy;
  108. else
  109. if this.montage.Mind==0
  110. this = getset(this, 'channels', 'X_plot2D', ind, val(1, :));
  111. this = getset(this, 'channels', 'Y_plot2D', ind, val(2, :));
  112. else
  113. this.montage.M(this.montage.Mind) = getset(this.montage.M(this.montage.Mind), 'channels', 'X_plot2D', ind, val(1, :));
  114. this.montage.M(this.montage.Mind) = getset(this.montage.M(this.montage.Mind), 'channels', 'Y_plot2D', ind, val(2, :));
  115. end
  116. res = this;
  117. end
  118. function xy = grid(n)
  119. ncol = ceil(sqrt(n));
  120. x = 0:(1/(ncol+1)):1;
  121. x = 0.9*x+0.05;
  122. x = x(2:(end-1));
  123. y = fliplr(x);
  124. [X, Y] = meshgrid(x, y);
  125. xy = [X(1:n); Y(1:n)];
  126. function xy = shiftxy(xy,mindist)
  127. x = xy(1,:);
  128. y = xy(2,:);
  129. l=1;
  130. i=1; %filler
  131. mindist = mindist/0.999; % limits the number of loops
  132. while (~isempty(i) && l<50)
  133. xdiff = repmat(x,length(x),1) - repmat(x',1,length(x));
  134. ydiff = repmat(y,length(y),1) - repmat(y',1,length(y));
  135. xydist= sqrt(xdiff.^2 + ydiff.^2); %euclidean distance between all sensor pairs
  136. [i,j] = find(xydist<mindist*0.999);
  137. rm=(i<=j); i(rm)=[]; j(rm)=[]; %only look at i>j
  138. for m = 1:length(i);
  139. if (xydist(i(m),j(m)) == 0)
  140. diffvec = [mindist./sqrt(2) mindist./sqrt(2)];
  141. else
  142. xydiff = [xdiff(i(m),j(m)) ydiff(i(m),j(m))];
  143. diffvec = xydiff.*mindist./xydist(i(m),j(m)) - xydiff;
  144. end
  145. x(i(m)) = x(i(m)) - diffvec(1)/2;
  146. y(i(m)) = y(i(m)) - diffvec(2)/2;
  147. x(j(m)) = x(j(m)) + diffvec(1)/2;
  148. y(j(m)) = y(j(m)) + diffvec(2)/2;
  149. end
  150. l = l+1;
  151. end
  152. xy = [x; y];