Skip to content

Commit 969c81b

Browse files
committedApr 28, 2019
Change interface to plotvol.
1 parent b11b316 commit 969c81b

File tree

2 files changed

+68
-29
lines changed

2 files changed

+68
-29
lines changed
 

‎plotvol.m

+51-29
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
%PLOTVOL Set the bounds for a 2D or 3D plot
22
%
3-
% PLOTVOL(W) creates a new axis, and sets the bounds for a 2D plot with X and Y spanning
4-
% the interval -W to W. The axes are labelled, grid is enabled, aspect ratio set to 1:1,
5-
% and hold is enabled for subsequent plots.
3+
% 2D plots::
4+
%
5+
% PLOTVOL([WX WY]) creates a new axis, and sets the bounds for a 2D plot,
6+
% with X spanning [-WX, WX] and Y spanning [-WY,WY].
67
%
78
% PLOTVOL([XMIN XMAX YMIN YMAX]) as above but the X and Y axis limits are explicitly provided.
89
%
9-
% PLOTVOL([XMIN XMAX YMIN YMAX ZMIN ZMAX]) as above but the X, Y and Z axis limits are
10+
% 3D plots::
11+
%
12+
% PLOTVOL(W) creates a new axis, and sets the bounds for a 3D plot with X, Y and Z spanning
13+
% the interval -W to W.
14+
%
15+
% PLOTVOL([WX WY WZ]) as above with X spanning [-WX, WX], Y spanning [-WY, WY] and Z
16+
% spanning [-WZ, WZ].
17+
%
18+
% Notes::
19+
% - The axes are labelled, grid is enabled, aspect ratio set to 1:1.
20+
% - Hold is enabled for subsequent plots.
21+
%
22+
% See also: axis.
23+
24+
%
25+
% PLOTVOL([XMIN XMAX YMIN YMAX ZMIN ZMAX]) as above but the X, Y and Z axis limits are
1026
% explicitly provided.
1127
%
1228
% See also axis, xaxis, yaxis.
1329

1430
% Copyright (C) 1993-2019 Peter I. Corke
1531
%
1632
% This file is part of The Spatial Math Toolbox for MATLAB (SMTB).
17-
%
33+
%
1834
% Permission is hereby granted, free of charge, to any person obtaining a copy
1935
% of this software and associated documentation files (the "Software"), to deal
2036
% in the Software without restriction, including without limitation the rights
@@ -25,7 +41,7 @@
2541
% The above copyright notice and this permission notice shall be included in all
2642
% copies or substantial portions of the Software.
2743
%
28-
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44+
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2945
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
3046
% FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
3147
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
@@ -35,34 +51,40 @@
3551
% https://github.com/petercorke/spatial-math
3652

3753
function plotvol(bounds)
38-
39-
if ~any(length(bounds) == [1 4 6])
40-
error('SMTB:plotvol:badarg', 'expecting vector of length 1, 4 or 6');
41-
end
42-
if length(bounds) == 1
43-
bounds = bounds * [-1 1 -1 1];
44-
end
4554

55+
assert(any(length(bounds) == [1 2 3 4 6]), 'SMTB:plotvol:badarg', 'expecting vector of length 1, 2, 3, 4 or 6');
56+
57+
58+
clf
4659
axis equal
47-
axis(bounds);
48-
49-
if length(bounds) == 4
50-
% 2D case
51-
52-
set(gca, 'XTick', floor(bounds(1)):floor(bounds(2)))
53-
set(gca, 'YTick', floor(bounds(3)):floor(bounds(4)))
54-
55-
elseif length(bounds) == 6
56-
% 3D case
57-
set(gca, 'XTick', floor(bounds(1)):floor(bounds(2)))
58-
set(gca, 'YTick', floor(bounds(3)):floor(bounds(4)))
59-
set(gca, 'ZTick', floor(bounds(5)):floor(bounds(6)))
60-
60+
61+
switch length(bounds)
62+
case 1
63+
bounds = bounds * [-1 1 -1 1 -1 1];
64+
case 3
65+
bounds = bounds(:) * [-1 1];
66+
bounds = bounds';
67+
bounds = bounds(:)';
68+
case 6
69+
% % 3D case
70+
% set(gca, 'XTick', floor(bounds(1)):floor(bounds(2)))
71+
% set(gca, 'YTick', floor(bounds(3)):floor(bounds(4)))
72+
% set(gca, 'ZTick', floor(bounds(5)):floor(bounds(6)))
73+
bounds = bounds(:)';
74+
case 2
75+
bounds = bounds(:) * [-1 1];
76+
bounds = bounds';
77+
bounds = bounds(:)';
78+
case 4
79+
bounds = bounds(:)';
6180
end
6281

82+
axis(bounds)
83+
6384
% common code
6485
grid on;
6586
hold on
66-
67-
87+
88+
6889
xyzlabel
90+
end

‎unit_test/PlotTest.m

+17
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,20 @@ function xyzlabel_test(tc)
202202
plot3(1, 2, 3)
203203
xyzlabel()
204204
end
205+
206+
function plotvol_test(tc)
207+
208+
% 2D cases
209+
plotvol([2 3])
210+
tc.verifyEqual(axis, [-2 2 -3 3]);
211+
plotvol([2 3 4 5])
212+
tc.verifyEqual(axis, [2 3 4 5]);
213+
214+
% 3D cases
215+
plotvol(3)
216+
tc.verifyEqual(axis, 3*[-1 1 -1 1 -1 1]);
217+
plotvol([1 2 3]);
218+
tc.verifyEqual(axis, [-1 1 -2 2 -3 3]);
219+
plotvol([1 2 3 4 5 6]);
220+
tc.verifyEqual(axis, [1 2 3 4 5 6]);
221+
end

0 commit comments

Comments
 (0)
Please sign in to comment.