Group uicontrol elements and ALIGN these GROUPS

I'm attempting to implement Grid, vertical and horizontal Layout classes in Matlab. To do so, I Group Buttons by putting them into Panels, where each Panel represents a Layout (e.g. a horizontal Layout). I have to group the buttons of one Layout, in order to be able to put one Layout into another (e.g. place a horizontal Layout in one cell of a grid layout). I have almost got it working, however, it seems, that Matlab's align-function doesn't Support Panels.
Is there a way of grouping Buttons and other uicontrol-objects and then aligning These Groups? It doesn't have to be uipanels.
Any help is highly appreciated.
Arduvast Gundar
P.S.: I know that this question has been asked before ( http://de.mathworks.com/matlabcentral/answers/245542-is-there-a-trick-to-align-uipanel-objects-w-e-g-uicontrol-objects ), however, the only answer provided was to manually set the Panel positions, which is not a satisfactory Option for me (I would hate to lose the comfort of align and the whole idea of the grid-layout class is to do away with Matlabs terrible absolute Position values and enable auto-positioning).

回答(1 个)

So, this is what I've come up with so far, following the absolute-positioning-Approach. However, I would appreciate a more elegant way of doing this.
function [newPosition] = align (obj,p_elems,varargin)
currPos = get(p_elems,'Position');
currPos = cell2mat(currPos);
% Sort Elements:
elemIndex = [1:numel(p_elems)];
elemIndex = elemIndex';
currPos = [currPos elemIndex];
l2rPos = sortrows(currPos);
t2bPos = sortrows(currPos,2);
noElems = numel(p_elems);
% get extreme values
[maxLeft,lIdx] = max(currPos(:,1));
[maxRight,rIdx] = max((currPos(:,1)+ currPos(:,3)));
maxWidth = abs(maxRight - maxLeft);
[maxBottom,bIdx] = max(currPos(:,2));
[maxTop,tIdx] = max((currPos(:,2)+ currPos(:,4)));
maxHeight = abs(maxTop - maxBottom);
xAlignMode = varargin{1};
yAlignMode = '';
xSpacing = [];
ySpacing = [];
vaIdx = [1 0 2 0]; % xAlignMode, xSpacing, YAlignMode, ySpacing
if strcmp(xAlignMode,'Fixed')
vaIdx(2) = 2;
vaIdx(3) = 3;
vaIdx(4) = 4;
else
vaIdx(4) = 3;
end
yAlignMode = varargin{vaIdx(3)};
switch xAlignMode
case 'Left'
newPosition(:,1) = currPos(1,1);
case 'Center'
newPosition(:,1) = maxWidth/2 - (currPos(:,1)-currPos(:,3))/2;
case 'Right'
newPosition(:,1) = currPos(1,1) + currPos(1,3) - currPos(:,3);
case 'Distribute'
% Total width of Elements:
totalWidth = sum(l2rPos(:,3));
unusedSpace = maxWidth - totalWidth;
spacer = 4; % default value
if unusedSpace > 0
spacer = unusedSpace/noElems;
end
tmpX = l2rPos(1,3) + spacer;
for iElem = 2:noElems
l2rPos(iElem,1) = tmpX;
tmpX = tmpX + l2rPos(iElem,3) + spacer;
end
newPosition = l2rPos;
case 'Fixed'
spacer = varargin{vaIdx(2)};
tmpX = l2rPos(1,3) + spacer;
for iElem = 2:noElems
l2rPos(iElem,1) = tmpX;
tmpX = tmpX + l2rPos(iElem,3) + spacer;
end;
newPosition = l2rPos;
case 'None'
newPosition = currPos;
otherwise
Error('Incorrect Parameter for horizontal alignment mode');
end
switch yAlignMode
case 'Bottom'
newPosition(:,2) = currPos(1,2);
case 'Middle'
newPosition(:,2) = maxWidth/2 - (currPos(:,2)-currPos(:,4))/2;
case 'Top'
newPosition(:,2) = currPos(1,2) + currPos(1,4) - currPos(:,4);
case 'Distribute'
noElems = numel(p_elems);
% Total width of Elements:
totalHeight = sum(t2bPos(:,4));
unusedSpace = maxHeight - totalHeight;
spacer = 4; % default value
if unusedSpace > 0
spacer = unusedSpace/noElems;
end
tmpY = t2bPos(1,4) + spacer;
for iElem = 2:noElems
t2bPos(iElem,1) = tmpY;
tmpY = tmpY + t2bPos(iElem,4) + spacer;
end
newPosition = t2bPos;
case 'Fixed'
spacer = varargin{vaIdx(4)};
tmpY = t2bPos(1,4) + spacer;
for iElem = 2:noElems
t2bPos(iElem,2) = tmpY;
tmpY = tmpY + t2bPos(iElem,4) + spacer;
end;
newPosition = t2bPos;
case 'None'
newPosition = currPos;
otherwise
Error('Incorrect Parameter for horizontal alignment mode');
end
newPosition = sortrows(newPosition,5);
newPosition = newPosition(:,1:4);
end

类别

帮助中心File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by