📄 som_grid.m
字号:
munits=prod(S.msize);% Default coordinates according to negihborhoodif isempty(S.coord), if ischar(S.lattice), switch S.lattice, case{'hexa','rect'} S.coord=som_vis_coords(S.lattice,S.msize); otherwise error('String value for lattice must be ''hexa'' or ''rect''.'); end else error('Lattice is not ''hexa'' or ''rect'': coordinates must be given.'); endend% connectionstype=class(S.lattice);switch typecase {'sparse','double'} % free topology fixedline=False; case 'char' % default topologies (hexa,char) switch S.lattice case 'hexa' hexa=True; case 'rect' hexa=False; otherwise error('Unknown lattice or neighborhood.'); end % If topology is hexa/rect but linetype, color etc. is % not constant, the topology is set to free if size(S.linewidth,1)>1 | size(S.linecolor,1)>1 | ... iscell(S.linecolor) % matrix or cell = not constant fixedline=False; S.lattice=som_connection({S.lattice,S.msize,S.shape}); else fixedline=True; endend% Check coordinate matrix size and set dummy zeros to z-axis% if 2D coordinates (always 3D plots!)if ~vis_valuetype(S.coord,{[munits 2],[munits 3]}), error('Coordinate matrix has wrong size.');elseif size(S.coord,2)==2, S.coord(:,3)=0;end% Fixed marker size, color, type? if size(S.markersize,1)>1 | size(S.markercolor,1)>1 | size(S.marker,1)>1 fixedmarker=False;else fixedmarker=True;end% Check labelsif ~vis_valuetype(S.label,{'chararray','2Dcellarray_of_char'}) ... & ~isempty(S.label), error('Labels should be in a char array or cell array of strings.');elseif ischar(S.label) S.label=cellstr(S.label);endif size(S.label,1) ~= munits & ~isempty(S.label), error('Number of labels and map size do not match.');end% Check line width, marker size, marker color,% label size label color and surf sizes&types:if ~vis_valuetype(S.linewidth,{[munits munits] [1 1]}), error('LineWidth matrix value has wrong size or dimension.');elseif any(S.linewidth(:)<0), error('All elements of LineWidth must be non-negative.');elseif ~vis_valuetype(S.markersize,{[munits 1] [1 1]}), error('MarkerSize matrix value has wrong size or dimension.');elseif any(S.markersize(:)<0), error('All elements of MarkerSize must be non-negative.');elseif ~vis_valuetype(S.markercolor,{'1x3rgb','colorstyle'}) & ... ~vis_valuetype(S.markercolor,{[munits 3],'nx3rgb'},'all'), error('MarkerColor should be a ColorSpec or Mx3 matrix of RGB triples.');elseif ~vis_valuetype(S.labelcolor,{'1x3rgb','colorstyle','xor'}), error('LabelColor shoud be a ColorSpec or ''xor'' or ''none''.')elseif ~vis_valuetype(S.labelsize,{'1x1'}) error('LabelSize should be a scalar.');elseif ~isempty(S.surf) & ~vis_valuetype(S.surf,{[munits 1] [munits 3]}); error('Surf matrix value has wrong size or dimension.');end% Check marker type & sizeif vis_valuetype(S.marker,{'cellcolumn_of_char'}) % Don't bother to check the mareker strings in this case % let the plot3 handle them; it returns quite understandable % error messages, anyway if ~size(S.marker) == [munits 1], error(['Marker should be one of Matlab''s valid marker type,' ... ' string ''none'' or a Mx1 cell array of these.']); endelseif ~vis_valuetype(S.marker,{'markerstyle','none'}), error(['Marker should be one of Matlab''s valid marker type,' ... ' string ''none'' or a Mx1 cell array of these.']); end% Check line type & size: only one line style allowedif ~vis_valuetype(S.line,{'linestyle','none'}) error(['Line should be a valid Matlab''s line style string or' ... ' string ''none''.']);end % Check line colorif iscell(S.linecolor), if ndims(S.linecolor) ~= 2 | any(size(S.linecolor) ~= [1 3]), error('Cell input for LineColor should be of form {r,g,b}.') elseif ~vis_valuetype(S.linecolor{1},{[munits munits],'nxn[0,1]'},'all')| ... ~vis_valuetype(S.linecolor{2},{[munits munits],'nxn[0,1]'},'all')| ... ~vis_valuetype(S.linecolor{3},{[munits munits],'nxn[0,1]'},'all'), error(['In cell input {r,g,b} some matrix r,g or b is invalid: ' ... 'Size must be MxM and values in interval [0,1].']); end elseif ~vis_valuetype(S.linecolor,{'colorstyle','1x3rgb'}) & ... ~vis_valuetype(S.linecolor,{'nxnx3rgb', [munits munits 3]},'all'), error('Invalid LineColor: see help text for valid values.'),elseif vis_valuetype(S.linecolor, {'none'}), error('LineColor ''none'' not allowed: set Line to ''none'' instead.');end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Actionmemhold=ishold; % take hold stateif ~memhold cla;endhold on;% Set surf if it existif ~isempty(S.surf), for i=1:3, s(:,:,i)=reshape(S.coord(:,i),S.msize); end s(:,:,4:3+size(S.surf,2))=reshape(S.surf,[S.msize size(S.surf,2)]); s=surf(s(:,:,1),s(:,:,2),s(:,:,3),s(:,:,4:end)); set(s,'EdgeColor','none','Marker','none','FaceColor','interp');endif fixedline, % Line properties are fixed: draw fast, but % if line is set to 'none' set empty handle ans skip if strcmp(S.line,'none') l={}; else p1=reshape(S.coord, [S.msize 3]); p2=zeros(size(p1)-[0 1 0]); p2(1:2:end,:,:)=p1(1:2:end,2:end,:); p2(2:2:end,:,:)=p1(2:2:end,1:end-1,:); l{1}=plot3(p1(:,:,1), p1(:,:,2), p1(:,:,3), ... 'Color', S.linecolor(1,:), ... 'LineWidth', S.linewidth(1), ... 'LineStyle', S.line); l{2}=plot3(p1(:,:,1)', p1(:,:,2)', p1(:,:,3)', ... 'Color', S.linecolor(1,:), ... 'LineWidth', S.linewidth(1), ... 'LineStyle', S.line); if hexa, l{3}=plot3(p2(:,:,1), p2(:,:,2), p2(:,:,3), ... 'Color', S.linecolor(1,:), ... 'LineWidth', S.linewidth(1), ... 'LineStyle', S.line); end end l=cat(1,l{:});else % Variable properties: draw connection by connection [I,J,lw]=find(S.lattice); x=[S.coord(I,1)'; S.coord(J,1)']; y=[S.coord(I,2)'; S.coord(J,2)']; z=[S.coord(I,3)'; S.coord(J,3)']; if S.linewidth(1)==0, linewidth=0.5; else linewidth=S.linewidth(1); end if ndims(S.linecolor) ~= 3 if isstr(S.linecolor) l=plot3(x, y, z, ... 'Color', S.linecolor, ... 'LineWidth', linewidth, ... 'LineStyle',S.line); else if iscell(S.linecolor) lcolor=[S.linecolor{1}(1,1) S.linecolor{2}(1,1) S.linecolor{3}(1,1)]; l=plot3(x, y, z, ... 'Color', lcolor, ... 'LineWidth', linewidth, ... 'LineStyle',S.line); else l=plot3(x, y, z, ... 'Color', S.linecolor(1,:), ... 'LineWidth', linewidth, ... 'LineStyle',S.line); end end else l=plot3(x, y, z, ... 'Color', S.linecolor(1,1,:), ... 'LineWidth', linewidth, ... 'LineStyle',S.line); endendif fixedmarker, % If marker is set to 'none' skip and set empty handle if strcmp(S.marker,'none') m=[]; else % Fixed markers: draw all in one command m=plot3(S.coord(:,1), S.coord(:,2), S.coord(:,3), ... 'LineStyle', 'none', ... 'Marker', S.marker, ... 'MarkerSize', S.markersize(1), ... 'MarkerFaceColor', S.markercolor(1,:), ... 'MarkerEdgeColor', S.markercolor(1,:)); endelse % Variable marker properties: draw marker by marker x=[S.coord(:,1)'; S.coord(:,1)']; y=[S.coord(:,2)'; S.coord(:,2)']; z=[S.coord(:,3)'; S.coord(:,3)']; if iscell(S.marker) marker=S.marker{1}; else marker=S.marker(1); end sz=max(S.markersize(1),0.1); m=plot3(x, y, z, ... 'LineStyle', 'none', ... 'Marker', marker, ... 'MarkerSize', sz, ... 'MarkerFaceColor', S.markercolor(1,:), ... 'MarkerEdgeColor', S.markercolor(1,:));endL=length(l); n=munits;%%% Set variable properties %%%% Line widthif length(S.linewidth)>1 lwidth=diag(S.linewidth(I,J)); % Handle zero width iszero=(lwidth == 0);lwidth(iszero)=0.5; for i=1:length(l), set(l(i),'LineWidth', lwidth(i)); end if ~isempty(iszero), % zero width set(l(iszero),'Visible','off'); endend% Line colorif size(S.linecolor,1)>1 | iscell(S.linecolor) if length(size(S.linecolor)) == 3 | iscell(S.linecolor) if ~iscell(S.linecolor) for i=1:L set(l(i),'Color',S.linecolor(I(i),J(i),:)); end else for i=1:L lcolor=[S.linecolor{1}(I(i),J(i)),... S.linecolor{2}(I(i),J(i)),... S.linecolor{3}(I(i),J(i))]; set(l(i),'Color',lcolor); end end else for i=1:L, set(l(i),'Color', S.linecolor(I(i),:)); end endend% Marker sizeif length(S.markersize)>1 % handle zero size iszero=find(~S.markersize); S.markersize(iszero)=1; for i=1:n, set(m(i),'MarkerSize', S.markersize(i)); end if ~isempty(iszero), % zero size set(m(iszero),'Visible','off'); endend% Marker typeif size(S.marker,1)>1 S.marker=char(S.marker); for i=1:n, set(m(i),'Marker', S.marker(i)); endend% Marker colorif size(S.markercolor,1)>1 for i=1:n, set(m(i),'MarkerFaceColor', S.markercolor(i,:), ... 'MarkerEdgeColor', S.markercolor(i,:)); endend% Set labels if they existif ~isempty(S.label) if vis_valuetype(S.labelcolor,{'xor'}), S.labelcolor='g'; XOR=1; else XOR=0; end if vis_valuetype(S.labelcolor,{'none'}), S.labelcolor='g'; VIS = 1; else VIS = 0; end for i=1:size(S.label,1), L=cat(1,S.label(i,:)); for j=length(L):-1:1, if isempty(L{j}), L=L(1:end-1); end end if isempty(L), L=''; end t(i)=text(S.coord(i,1), S.coord(i,2), S.coord(i,3), L,... 'FontSize', S.labelsize, 'Color',S.labelcolor, ... 'HorizontalAlignment', 'center'); end if XOR set(t,'EraseMode','xor'); end if VIS set(t,'Visible','off'); end else t=[];end%% Set hold stateif ~memhold, hold off; endif nargout==0, clear S m l t s;end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -