1 |
%VECPLOT routine to plot vectors. |
---|
2 |
% VECPLOT draws vectors on the current figure, at (x,y) |
---|
3 |
% locations determined in one of two ways. If the first |
---|
4 |
% argument to VECPLOT is a fem_grid_struct, then VECPLOT |
---|
5 |
% extracts the FEM domain coordinates as the vector origins. |
---|
6 |
% Otherwise, the user supplies the (x,y) coordinates |
---|
7 |
% explicitly. This flexibility exists because vector |
---|
8 |
% plotting does not require any knowledge of nodal |
---|
9 |
% connectivity, and perhaps the (u,v) data are not |
---|
10 |
% from a FEM model output. |
---|
11 |
% |
---|
12 |
% VECPLOT scales the magnitude of |
---|
13 |
% (u,v) by the magnitude of max(abs(u,v)) and then |
---|
14 |
% forces a vector of magnitude sc to be 10% of the x data |
---|
15 |
% range. By default, sc = 1., so that a 1 m/s vector will |
---|
16 |
% be scaled to 10% of the x data range. If sc=.5, then |
---|
17 |
% a vector magnitude of 50 cm/s will be scaled to 10% of the |
---|
18 |
% x data range. Decreasing sc serves to make the vectors |
---|
19 |
% appear larger. VECPLOT then prompts the user to place |
---|
20 |
% the vector scale on the figure, unless scale_xor,scale_yor |
---|
21 |
% is specified (see below). |
---|
22 |
% |
---|
23 |
% INPUT: fem_grid_struct (from LOADGRID, see FEM_GRID_STRUCT) |
---|
24 |
% OR |
---|
25 |
% x,y - vector origins |
---|
26 |
% u,v - vector amplitudes |
---|
27 |
% These inputs are optional, but if one is needed, all |
---|
28 |
% preceeding it wil be required. |
---|
29 |
% sc - vector scaler; (optional; default = 1.) |
---|
30 |
% sclab - label for vector scale; (optional; default = 'cm/s') |
---|
31 |
% scale_xor,scale_yor - location to place vector scale |
---|
32 |
% |
---|
33 |
% OUTPUT: h - vector of handles to the vector lines drawn, the |
---|
34 |
% scale vector, and the scale vector text. |
---|
35 |
% |
---|
36 |
% NOTES: VECPLOT requires atleast 2 coordinates and vectors. |
---|
37 |
% |
---|
38 |
% CALL: hv=vecplot(x,y,u,v,sc,sclab) |
---|
39 |
% or |
---|
40 |
% hv=vecplot(fem_grid_struct,u,v,sc,sclab) |
---|
41 |
% |
---|
42 |
% Written by : Brian O. Blanton |
---|
43 |
% |
---|
44 |
function retval=vecplot(varargin) |
---|
45 |
|
---|
46 |
% Copy incoming cell array |
---|
47 |
input_cell=varargin; |
---|
48 |
|
---|
49 |
% DEFINE ERROR STRINGS |
---|
50 |
err1=['Invalid number of input arguments to VECPLOT']; |
---|
51 |
err2=['Struct to VECPLOT not a valid FEM_GRID_STRUCT.']; |
---|
52 |
err3=['Length of u,v must be the same']; |
---|
53 |
err4=['Length of (x,y) must equal length of (u,v).']; |
---|
54 |
err5=['Alteast 3 arguments are required if first is a fem_grid_struct.']; |
---|
55 |
err6=['Alteast 4 arguments are required if first two are x,y.']; |
---|
56 |
err7=['Second optional argument (sclab) must be a string.']; |
---|
57 |
err8=['Both x- and y-coords of vector scale must be specified.']; |
---|
58 |
|
---|
59 |
nargs=length(input_cell); |
---|
60 |
|
---|
61 |
% PROCESS THE INPUT ARGUMENTS |
---|
62 |
% Length of input_cell must be between 3 and 8, inclusive. |
---|
63 |
if nargs<3 | nargs>8 |
---|
64 |
error(err1) |
---|
65 |
end |
---|
66 |
% Determine if first cell contents of input_cell is |
---|
67 |
% a struct and if so, is it a valid fem_grid_struct |
---|
68 |
if isstruct(input_cell{1}) |
---|
69 |
% Is the input struct FEM valid? |
---|
70 |
if ~is_valid_struct(input_cell{1}) |
---|
71 |
error(err2) |
---|
72 |
end |
---|
73 |
if nargs<3 |
---|
74 |
error(err5) |
---|
75 |
end |
---|
76 |
% If we're here, the first cell of input_cell is the domain |
---|
77 |
% structure; |
---|
78 |
xin=input_cell{1}.x; |
---|
79 |
yin=input_cell{1}.y; |
---|
80 |
% The second and third cells are u,v |
---|
81 |
uin=input_cell{2};vin=input_cell{3}; |
---|
82 |
% Check lengths of u,v against x |
---|
83 |
if length(uin)~=length(vin) |
---|
84 |
error(err3) |
---|
85 |
end |
---|
86 |
if length(uin)~=length(xin) |
---|
87 |
error(err4) |
---|
88 |
end |
---|
89 |
% delete the cells accounted for so far; the remaining will be |
---|
90 |
% processed below. |
---|
91 |
input_cell(3)=[]; |
---|
92 |
input_cell(2)=[]; |
---|
93 |
input_cell(1)=[]; |
---|
94 |
else |
---|
95 |
% If we're here, the first argument to VECPLOT is NOT |
---|
96 |
% a structure; the first 4 arguments are then required |
---|
97 |
% and are x,y,u,v; there is no way to ensure that the |
---|
98 |
% order is correct. Hopefully, said user read the help text! |
---|
99 |
if nargs<4 |
---|
100 |
error(err6) |
---|
101 |
end |
---|
102 |
xin=input_cell{1};yin=input_cell{2}; |
---|
103 |
uin=input_cell{3};vin=input_cell{4}; |
---|
104 |
% delete the cells accounted for so far; the remaining will be |
---|
105 |
% processed below;. |
---|
106 |
input_cell(4)=[];input_cell(3)=[]; |
---|
107 |
input_cell(2)=[];input_cell(1)=[]; |
---|
108 |
end |
---|
109 |
|
---|
110 |
% At this point, the copy of the input cell has been reduced to |
---|
111 |
% contain the optional arguments, if any. |
---|
112 |
if length(input_cell)==0 |
---|
113 |
sc=1.;sclab=' cm/s'; |
---|
114 |
elseif length(input_cell)==1 |
---|
115 |
sc=input_cell{1}; |
---|
116 |
sclab=' cm/s'; |
---|
117 |
elseif length(input_cell)==2 |
---|
118 |
sc=input_cell{1}; |
---|
119 |
% Make sure second optional arg is char |
---|
120 |
if ~isa(input_cell{2},'char') |
---|
121 |
error(err7) |
---|
122 |
end |
---|
123 |
sclab=[' ' input_cell{2}]; |
---|
124 |
elseif length(input_cell)==3 |
---|
125 |
error(err8) |
---|
126 |
else |
---|
127 |
sc=input_cell{1}; |
---|
128 |
% Make sure second optional arg is char |
---|
129 |
if ~isa(input_cell{2},'char') |
---|
130 |
error(err7) |
---|
131 |
end |
---|
132 |
sclab=[' ' input_cell{2}]; |
---|
133 |
scale_xor=input_cell{3} ; |
---|
134 |
scale_yor=input_cell{4} ; |
---|
135 |
end |
---|
136 |
col='b'; |
---|
137 |
|
---|
138 |
% |
---|
139 |
% save the current value of the current figure's WindowButtonDownFcn, |
---|
140 |
% WindowButtonMotionFcn, and WindowButtonUpFcn |
---|
141 |
% |
---|
142 |
WindowButtonDownFcn=get(gcf,'WindowButtonDownFcn'); |
---|
143 |
WindowButtonMotionFcn=get(gcf,'WindowButtonMotionFcn'); |
---|
144 |
WindowButtonUpFcn=get(gcf,'WindowButtonUpFcn'); |
---|
145 |
set(gcf,'WindowButtonDownFcn',''); |
---|
146 |
set(gcf,'WindowButtonMotionFcn',''); |
---|
147 |
set(gcf,'WindowButtonUpFcn',''); |
---|
148 |
|
---|
149 |
|
---|
150 |
% SCALE VELOCITY DATA TO RENDERED WINDOW SCALE |
---|
151 |
% |
---|
152 |
RLs= get(gca,'XLim'); |
---|
153 |
xr=RLs(2)-RLs(1); |
---|
154 |
X1=RLs(1); |
---|
155 |
X2=RLs(2); |
---|
156 |
RLs= get(gca,'YLim'); |
---|
157 |
yr=RLs(2)-RLs(1); |
---|
158 |
Y1=RLs(1); |
---|
159 |
Y2=RLs(2); |
---|
160 |
|
---|
161 |
% IF RenderLimits NOT SET, USE RANGE OF DATA |
---|
162 |
% |
---|
163 |
if(xr==0|yr==0) |
---|
164 |
error('Axes must have been previously set for VECPLOT2 to work'); |
---|
165 |
end |
---|
166 |
pct10=xr/10; |
---|
167 |
|
---|
168 |
%FILTER DATA THROUGH VIEWING WINDOW |
---|
169 |
% |
---|
170 |
filt=find(xin>=X1&xin<=X2&yin>=Y1&yin<=Y2); |
---|
171 |
x=xin(filt); |
---|
172 |
y=yin(filt); |
---|
173 |
u=uin(filt); |
---|
174 |
v=vin(filt); |
---|
175 |
|
---|
176 |
% SCALE BY MAX VECTOR SIZE IN U AND V |
---|
177 |
% |
---|
178 |
us=u/sc; |
---|
179 |
vs=v/sc; |
---|
180 |
|
---|
181 |
% SCALE TO 10 PERCENT OF X RANGE |
---|
182 |
% |
---|
183 |
us=us*pct10; |
---|
184 |
vs=vs*pct10; |
---|
185 |
|
---|
186 |
% SEND VECTORS TO DRAWVEC ROUTINE |
---|
187 |
% |
---|
188 |
hp=drawvec(x,y,us,vs,25,col); |
---|
189 |
set(hp,'UserData',[xin yin uin vin]); |
---|
190 |
set(hp,'Tag','vectors'); |
---|
191 |
|
---|
192 |
% COLOR LARGEST VECTOR RED |
---|
193 |
%[trash,imax]=max(sqrt(us.*us+vs.*vs)); |
---|
194 |
%hvmax=drawvec(x(imax),y(imax),us(imax),vs(imax),25,'r'); |
---|
195 |
%set(hvmax,'Tag','scalearrow'); |
---|
196 |
|
---|
197 |
% PLACE SCALE WITH MOUSE ON SCREEN |
---|
198 |
% |
---|
199 |
if ~strcmp(blank(sclab),'no scale') |
---|
200 |
ptr=get(gcf,'Pointer'); |
---|
201 |
if ~exist('scale_xor')| ~exist('scale_yor') |
---|
202 |
disp('place scale on plot with a mouse button'); |
---|
203 |
[scale_xor,scale_yor]=ginput(1); |
---|
204 |
end |
---|
205 |
|
---|
206 |
ht1=drawvec(scale_xor,scale_yor,pct10,0.,25,'r'); |
---|
207 |
set(ht1,'Tag','scalearrow'); |
---|
208 |
if strcmp(blank(sclab),'m/s') |
---|
209 |
sctext=[num2str(sc) sclab]; |
---|
210 |
else |
---|
211 |
sctext=[num2str(100*sc) sclab]; |
---|
212 |
end |
---|
213 |
scaletext=text((scale_xor+scale_xor+pct10)/2,scale_yor-(Y2-Y1)*(.05),sctext); |
---|
214 |
set(scaletext,'HorizontalAlignment','center'); |
---|
215 |
set(scaletext,'VerticalAlignment','middle'); |
---|
216 |
set(scaletext,'Tag','scaletext'); |
---|
217 |
set(gcf,'Pointer',ptr); |
---|
218 |
|
---|
219 |
else |
---|
220 |
ht1=[];scaletext=[]; |
---|
221 |
end |
---|
222 |
|
---|
223 |
% OUTPUT IF DESIRED |
---|
224 |
% |
---|
225 |
if nargout==1,retval=[hp ht1 scaletext];,end |
---|
226 |
|
---|
227 |
|
---|
228 |
|
---|
229 |
% |
---|
230 |
% return the saved values of the current figure's WindowButtonDownFcn, |
---|
231 |
% WindowButtonMotionFcn, and WindowButtonUpFcn to the current figure |
---|
232 |
% |
---|
233 |
set(gcf,'WindowButtonDownFcn',WindowButtonDownFcn); |
---|
234 |
set(gcf,'WindowButtonMotionFcn',WindowButtonMotionFcn); |
---|
235 |
set(gcf,'WindowButtonUpFcn',WindowButtonUpFcn); |
---|
236 |
|
---|
237 |
% |
---|
238 |
% Brian O. Blanton |
---|
239 |
% Department of Marine Sciences |
---|
240 |
% 15-1A Venable Hall |
---|
241 |
% CB# 3300 |
---|
242 |
% Uni. of North Carolina |
---|
243 |
% Chapel Hill, NC |
---|
244 |
% 27599-3300 |
---|
245 |
% |
---|
246 |
% 919-962-4466 |
---|
247 |
% blanton@marine.unc.edu |
---|
248 |
% |
---|
249 |
% SUMMER 1998 |
---|
250 |
% |
---|