1 |
% |
---|
2 |
% COLORMESH2D draw a FEM mesh in 2-d colored by a scalar quantity. |
---|
3 |
% |
---|
4 |
% Input : elems - element list (.ele or .tri type) (required) |
---|
5 |
% x - x-coordinate list (required) |
---|
6 |
% y - y-coordinate list (required) |
---|
7 |
% Q - scalar to color with (optional) |
---|
8 |
% "nofill" - flag (optional) |
---|
9 |
% |
---|
10 |
% Output : hp - vector of handles, one for each element patch drawn. |
---|
11 |
% |
---|
12 |
% COLORMESH2D colors the mesh using the scalar Q. If Q |
---|
13 |
% is omitted from the argument list, COLORMESH2D draws |
---|
14 |
% the element connectivity in black and white. |
---|
15 |
% |
---|
16 |
% The flag "nofill" will prevent COLORMESH3D from filling |
---|
17 |
% the interior of surface triangles. |
---|
18 |
% |
---|
19 |
% NOTE: This is one of the few (2 or 3) OPNML MATLAB functions that |
---|
20 |
% deletes existing patch objects in the current axes before |
---|
21 |
% rendering the surface. This is to avoid having an unmanageable |
---|
22 |
% number of patch objects on the screen. |
---|
23 |
% |
---|
24 |
% COLORMESH2D sets the colormap to jet if it has not already |
---|
25 |
% been done. This colormap does not "wrap" around. |
---|
26 |
% |
---|
27 |
% Call as : hp=colormesh2d(elems,x,y,Q,'nofill') |
---|
28 |
% |
---|
29 |
function rv1=colormesh2d(elems,x,y,Q,sarg) |
---|
30 |
|
---|
31 |
% DEFINE ERROR STRINGS |
---|
32 |
err1=['more than 1 column in x-coordinate vector.']; |
---|
33 |
err2=['more than 1 column in y-coordinate vector.']; |
---|
34 |
err3=['node coordinate vectors must be the same length']; |
---|
35 |
err4=['scalar used to color must be 1-D']; |
---|
36 |
err5=['length of scalar must be the same length as coordinate vectors']; |
---|
37 |
err9=str2mat(' ','??? Error using ==> colormesh2d',... |
---|
38 |
'COLORMESH2D needs 3, 4, or 5 input arguments:',... |
---|
39 |
'Input : elems - element list (.ele or .tri type)',... |
---|
40 |
' x - x-coordinate list',... |
---|
41 |
' y - y-coordinate list',... |
---|
42 |
' Q - scalar to color with (optional)',... |
---|
43 |
' "nofill" - no-interior triangles flag (optional)',... |
---|
44 |
' '); |
---|
45 |
err10=str2mat(' ','??? Error using ==> colormesh2d',... |
---|
46 |
'COLORMESH2D optional flag argument must',... |
---|
47 |
'be the string "nofill".',... |
---|
48 |
'Type "help colormesh2d"',... |
---|
49 |
' '); |
---|
50 |
err11=str2mat(' ','??? Error using ==> colormesh2d',... |
---|
51 |
'COLORMESH2D optional flag argument "nofill"',... |
---|
52 |
'can only be used along with Q-specification.',... |
---|
53 |
'Type "help colormesh2d"',... |
---|
54 |
' '); |
---|
55 |
|
---|
56 |
if nargin <3|nargin >5 |
---|
57 |
disp(err9) |
---|
58 |
return |
---|
59 |
end |
---|
60 |
|
---|
61 |
% CHECK SIZE OF X-COORDINATE VECTOR |
---|
62 |
% NX = NUMBER OF X-COORDINATE VALUES, S = # OF COLS |
---|
63 |
% |
---|
64 |
[nx,s]=size(x); |
---|
65 |
if s~=1&nx~=1 |
---|
66 |
error(err1); |
---|
67 |
end |
---|
68 |
|
---|
69 |
% CHECK SIZE OF Y-COORDINATE VECTOR |
---|
70 |
% NY = NUMBER OF Y-COORDINATE VALUES, S = # OF COLS |
---|
71 |
% |
---|
72 |
[ny,s]=size(y); |
---|
73 |
if s~=1&ny~=1 |
---|
74 |
error(err2); |
---|
75 |
end |
---|
76 |
x=x(:); |
---|
77 |
y=y(:); |
---|
78 |
|
---|
79 |
if length(x) ~= length(y) |
---|
80 |
error(err3) |
---|
81 |
end |
---|
82 |
|
---|
83 |
if exist('Q') |
---|
84 |
if isstr(Q) |
---|
85 |
disp(err11) |
---|
86 |
return |
---|
87 |
end |
---|
88 |
if exist('sarg') |
---|
89 |
if ~strcmp(sarg,'nofill') |
---|
90 |
disp(err10) |
---|
91 |
return |
---|
92 |
end |
---|
93 |
end |
---|
94 |
|
---|
95 |
[nrowQ,ncolQ]=size(Q); |
---|
96 |
if ncolQ>1,error(err4);,end |
---|
97 |
if nrowQ ~= length(x) |
---|
98 |
error(err5) |
---|
99 |
end |
---|
100 |
Q=Q(:); |
---|
101 |
|
---|
102 |
[nelems,ncol]=size(elems); % nelems,ncol = number of elements, columns |
---|
103 |
if ncol==4 |
---|
104 |
elems=elems(:,2:4); |
---|
105 |
elseif ncol~=3 |
---|
106 |
errstr=str2mat(['COLORMESH2D is confused by the number of columns in '],... |
---|
107 |
['the element file. It must be only three (i1 i2 i3) '],... |
---|
108 |
['or four (node# i1 i2 i3)']); |
---|
109 |
disp('??? Error using ==> colormesh2d'); |
---|
110 |
disp(errstr); |
---|
111 |
return; |
---|
112 |
end |
---|
113 |
|
---|
114 |
elems=elems'; |
---|
115 |
[m,n]=size(elems); |
---|
116 |
xt=x(elems); |
---|
117 |
xt=reshape(xt,m,n); |
---|
118 |
yt=y(elems); |
---|
119 |
yt=reshape(yt,m,n); |
---|
120 |
Qt=Q(elems); |
---|
121 |
Qt=reshape(Qt,m,n); |
---|
122 |
|
---|
123 |
% delete previous colorsurf objects |
---|
124 |
% delete(findobj(gca,'Type','patch','Tag','colorsurf')) |
---|
125 |
rh_ch=get(0,'Children'); |
---|
126 |
ColorsurfFigure=findobj(rh_ch,'flat','Type','figure','Tag','ColorsurfFigure'); |
---|
127 |
if ~isempty(ColorsurfFigure) |
---|
128 |
ud=get(ColorsurfFigure,'UserData'); |
---|
129 |
if ~isempty(ud) |
---|
130 |
if isobj(ud(1)),delete(ud);,end |
---|
131 |
delete(ColorsurfFigure); |
---|
132 |
end |
---|
133 |
end |
---|
134 |
|
---|
135 |
colormap(jet); |
---|
136 |
% colormenu2; |
---|
137 |
if exist('sarg') |
---|
138 |
if strcmp(sarg,'nofill') |
---|
139 |
hp=patch(xt,yt,Qt,'EdgeColor','interp',... |
---|
140 |
'FaceColor','none','Tag','colorsurf'); |
---|
141 |
end |
---|
142 |
else |
---|
143 |
hp=patch(xt,yt,Qt,'EdgeColor','none','FaceColor','interp','Tag','colorsurf'); |
---|
144 |
end |
---|
145 |
|
---|
146 |
% hp=patch(xt,yt,Qt,'EdgeColor','none','FaceColor','interp','Tag','colorsurf'); |
---|
147 |
% Create invisible figure; store patch handles in figure's UserData. |
---|
148 |
% fig=gcf; |
---|
149 |
% figure('Visible','off','UserData',hp,'Tag','ColorsurfFigure'); |
---|
150 |
% figure(fig); |
---|
151 |
|
---|
152 |
% if isempty(findobj(gcf,'Type','axes','Tag','colorbar')) |
---|
153 |
% hc=colorbar('horiz'); |
---|
154 |
% set(hc,'Tag','colorbar'); |
---|
155 |
% else |
---|
156 |
% colorbar; |
---|
157 |
% end |
---|
158 |
else |
---|
159 |
hp=drawelems(elems,x,y); |
---|
160 |
end |
---|
161 |
|
---|
162 |
% Output if requested. |
---|
163 |
if nargout==1,rv1=hp;,end |
---|
164 |
|
---|