-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributeObjects.m
51 lines (47 loc) · 1.74 KB
/
distributeObjects.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [objpos,objdim] = distributeObjects(nobjects,startpos,endpos,gap,warnoff)
%[objpos, objdim] = distributeObjects(nobjects, startpos, endpos, gap, warnoff)
%
%Returns the proper positions and size for uniformly spaced GUI objects.
%
%Enter the number of objects (nobjects), the starting position (startpos),
%the end position (endpos), and the gap, and this function will return a
%vector of starting points (objpos) as well as a dimension for all
%uniformly sized, equally spaced objects (buttons, boxes, etc.).
%
%This works for horizontal OR vertical distribution of items, as long as
%startpos < endpos, and for normalized or any other kind of unit .
%
% E.g., To uniformly distributeObjects 4 buttons horizontally starting at 10
% pixels, ending at 100 pixels, and with a gap of 5 pixels,
% [objpos,objdim] = distributeObjects(4,10,100,5) returns
% objpos = [10.0000 33.7500 57.5000 81.2500],
% objdim = 18.7500
% Thus, your GUI buttons should be positioned at
% [objpos(1) y objdim height], [objpos(2) y objdim height],....
%
%Written by Brett Shoelson, PhD
%12/09/03
%brett.shoelson@mathworks.com
%
% Copyright 2013 MathWorks, Inc.
if nargin<5
warnoff = 0;
end
rev = 0;
if startpos > endpos
rev = 1;
tmp = endpos;
endpos = startpos;
startpos = tmp;
end
objdim = ((endpos-startpos)-(nobjects-1)*gap)/nobjects;
objpos = startpos:objdim+gap:endpos;
%Account for case of gap==0, which generates a starting point at the end of
%the object range.
objpos = objpos(1:nobjects);
if rev
objpos = objpos(end:-1:1);
end
if ~warnoff && (any(objpos < 0) || objdim < 0)
warndlg('The parameters you entered result in a negative starting point or dimension. You may want to rethink that.');
end