comparison scripts/plot/feather.m @ 17483:bddb9688e41c

feather.m: Overhaul function for Matlab visual compatibility. * scripts/plot/feather.m: Reduce length of arrowhead size to 0.20 of arrow shaft. Change arrow head angle with shaft from 45 to 30 degrees. Use variable 'h' instead of 'retval' to match documentation. Redo input validation. Add %!error input validation tests.
author Rik <rik@octave.org>
date Tue, 24 Sep 2013 13:17:04 -0700
parents 177147bf7b55
children
comparison
equal deleted inserted replaced
17482:997b700b6ad4 17483:bddb9688e41c
46 ## @end example 46 ## @end example
47 ## 47 ##
48 ## @seealso{plot, quiver, compass} 48 ## @seealso{plot, quiver, compass}
49 ## @end deftypefn 49 ## @end deftypefn
50 50
51 function retval = feather (varargin) 51 function h = feather (varargin)
52 52
53 [hax, varargin, nargin] = __plt_get_axis_arg__ ("feather", varargin{:}); 53 [hax, varargin, nargin] = __plt_get_axis_arg__ ("feather", varargin{:});
54 54
55 arrowsize = 0.25; 55 if (nargin == 0 || nargin > 3)
56 print_usage ();
57 endif
56 58
57 if (nargin == 0) 59 if (nargin == 1 || (nargin == 2 && ! isnumeric (varargin{2})))
58 print_usage ();
59 elseif (nargin == 1 || (nargin == 2 && ! isnumeric (varargin{2})))
60 ioff = 2;
61 z = varargin{1}(:).'; 60 z = varargin{1}(:).';
62 u = real (z); 61 u = real (z);
63 v = imag (z); 62 v = imag (z);
64 elseif (nargin > 1 && isnumeric (varargin{2})) 63 have_line_spec = (nargin == 2);
64 elseif (nargin >= 2 && isnumeric (varargin{2}))
65 ioff = 3; 65 ioff = 3;
66 u = varargin{1}(:).'; 66 u = varargin{1}(:).';
67 v = varargin{2}(:).'; 67 v = varargin{2}(:).';
68 have_line_spec = (nargin == 3);
69 else
70 print_usage ();
68 endif 71 endif
69 72
70 line_spec = "b-"; 73 arrowsize = 0.20;
71 have_line_spec = false; 74 line_spec = "-b";
72 while (ioff <= nargin) 75
73 arg = varargin{ioff++}; 76 if (have_line_spec)
74 if ((ischar (arg) || iscellstr (arg)) && ! have_line_spec) 77 arg = varargin{end};
75 [linespec, valid] = __pltopt__ ("feather", arg, false); 78 if (ischar (arg) || iscellstr (arg))
79 [~, valid] = __pltopt__ ("feather", arg, false);
76 if (valid) 80 if (valid)
77 line_spec = arg; 81 line_spec = arg;
78 have_line_spec = false;
79 break;
80 else 82 else
81 error ("feather: invalid linespec"); 83 error ("feather: invalid linestyle STYLE");
82 endif 84 endif
83 else 85 else
84 error ("feather: unrecognized argument"); 86 error ("feather: invalid linestyle STYLE");
85 endif 87 endif
86 endwhile 88 endif
87 89
88 ## Matlab draws feather plots, with the arrow head as one continous 90 ## Matlab draws feather plots, with the arrow head as one continous
89 ## line, and each arrow separately. This is completely different than 91 ## line, and each arrow separately. This is completely different from
90 ## quiver and quite ugly. 92 ## quiver and quite ugly.
91 n = length (u); 93 n = length (u);
92 xend = [1 : n] + u; 94 xend = [1 : n] + u;
93 xtmp = [1 : n] + u .* (1 - arrowsize); 95 xtmp = [1 : n] + u .* (1 - arrowsize);
94 yend = v; 96 yend = v;
95 ytmp = v .* (1 - arrowsize); 97 ytmp = v .* (1 - arrowsize);
96 x = [[1 : n]; xend; xtmp - v * arrowsize; xend; ... 98 x = [[1 : n]; xend; xtmp - v * arrowsize / 3; xend; ...
97 xtmp + v * arrowsize]; 99 xtmp + v * arrowsize / 3];
98 y = [zeros(1, n); yend; ytmp + u * arrowsize / 3; yend; ... 100 y = [zeros(1, n); yend; ytmp + u * arrowsize / 3; yend; ...
99 ytmp - u * arrowsize / 3]; 101 ytmp - u * arrowsize / 3];
100 102
101 oldfig = []; 103 oldfig = [];
102 if (! isempty (hax)) 104 if (! isempty (hax))
103 oldfig = get (0, "currentfigure"); 105 oldfig = get (0, "currentfigure");
110 set (0, "currentfigure", oldfig); 112 set (0, "currentfigure", oldfig);
111 endif 113 endif
112 end_unwind_protect 114 end_unwind_protect
113 115
114 if (nargout > 0) 116 if (nargout > 0)
115 retval = hlist; 117 h = hlist;
116 endif 118 endif
117 119
118 endfunction 120 endfunction
119 121
120 122
123 %! phi = [0 : 15 : 360] * pi/180; 125 %! phi = [0 : 15 : 360] * pi/180;
124 %! feather (sin (phi), cos (phi)); 126 %! feather (sin (phi), cos (phi));
125 %! axis tight; 127 %! axis tight;
126 %! title ('feather plot'); 128 %! title ('feather plot');
127 129
130 %% Test input validation
131 %!error feather ()
132 %!error feather (1,2,3,4)
133 %!error feather (1, "-r", 2)
134 %!error <invalid linestyle STYLE> feather (1, "abc")
135 %!error <invalid linestyle STYLE> feather (1, {1})
136