from Align blocks tool by Tijil Thomas
This is a very useful tool to align blocks and distribute the distance between the blocks.

AlignBlocks(varargin)
function varargout = AlignBlocks(varargin)
%%% 

% #####################################

% Author: Tijil Thomas

% Last Modified by GUIDE v2.5 21-Apr-2009 11:41:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @AlignBlocks_OpeningFcn, ...
                   'gui_OutputFcn',  @AlignBlocks_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before AlignBlocks is made visible.
function AlignBlocks_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to AlignBlocks (see VARARGIN)

global nDistV
global checkVselected
global checkHselected
checkHselected = 0;
checkVselected = 0;
nDistV = 20;

set(handles.pushbutton_v2, 'CData', imread('vetical2.bmp'));
set(handles.pushbutton_v3, 'CData', imread('vetical3.bmp'));
set(handles.pushbutton_v4, 'CData', imread('vetical4.bmp'));
set(handles.pushbutton_v5, 'CData', imread('vetical5.bmp'));
set(handles.pushbutton_v6, 'CData', imread('vetical6.bmp'));
set(handles.pushbutton_v7, 'CData', imread('vetical7.bmp'));
set(handles.pushbutton_v8, 'CData', imread('vetical8.bmp'));


set(handles.pushbutton_h2, 'CData', imread('horizontal_2.bmp'));
set(handles.pushbutton_h3, 'CData', imread('horizontal_3.bmp'));
set(handles.pushbutton_h4, 'CData', imread('horizontal_4.bmp'));
set(handles.pushbutton_h5, 'CData', imread('horizontal_5.bmp'));
set(handles.pushbutton_h6, 'CData', imread('horizontal_6.bmp'));
set(handles.pushbutton_h7, 'CData', imread('horizontal_7.bmp'));
set(handles.pushbutton_h8, 'CData', imread('horizontal_8.bmp'));

% button pressed in vertical panel
handles.button_pressed_v = handles.pushbutton_v1;
% button pressed in horizontal panel
handles.button_pressed_h = handles.pushbutton_h1;

% Choose default command line output for AlignBlocks
handles.output = hObject;

% Initialize the edit box for vertical
set(handles.edit_pixcel_v, 'Enable', 'off');
set(handles.checkbox_spacing_v, 'Enable', 'off');
set(handles.pixcel_v, 'Enable', 'off');
set(handles.pushbutton_v1, 'Enable', 'off');
set(handles.pushbutton_v2, 'Enable', 'on');
set(handles.pushbutton_v3, 'Enable', 'on');
set(handles.pushbutton_v4, 'Enable', 'on');
set(handles.pushbutton_v5, 'Enable', 'on');
set(handles.pushbutton_v6, 'Enable', 'on');
set(handles.pushbutton_v7, 'Enable', 'on');
set(handles.pushbutton_v8, 'Enable', 'on');

set(handles.checkbox_spacing_v, 'Value', 0);

% Initialize the edit box for horizontal
set(handles.edit_pixcel_h, 'Enable', 'off');
set(handles.checkbox_spacing_h, 'Enable', 'off');
set(handles.pixcel_h, 'Enable', 'off');
set(handles.pushbutton_h1, 'Enable', 'off');
set(handles.pushbutton_h2, 'Enable', 'on');
set(handles.pushbutton_h3, 'Enable', 'on');
set(handles.pushbutton_h4, 'Enable', 'on');
set(handles.pushbutton_h5, 'Enable', 'on');
set(handles.pushbutton_h6, 'Enable', 'on');
set(handles.pushbutton_h7, 'Enable', 'on');
set(handles.pushbutton_h8, 'Enable', 'on');

set(handles.checkbox_spacing_h,'Value', 0);
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes AlignBlocks wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = AlignBlocks_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes on button press in pushbutton_v1.
function pushbutton_v1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'off');
set(handles.pixcel_v, 'Enable', 'off');
set(handles.edit_pixcel_v, 'Enable', 'off');

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_v2.
function pushbutton_v2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'off');
set(handles.pixcel_v, 'Enable', 'off');
set(handles.edit_pixcel_v, 'Enable', 'off');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_v3.
function pushbutton_v3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'off');
set(handles.pixcel_v, 'Enable', 'off');
set(handles.edit_pixcel_v, 'Enable', 'off');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_v4.
function pushbutton_v4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'off');
set(handles.pixcel_v, 'Enable', 'off');
set(handles.edit_pixcel_v, 'Enable', 'off');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_v5.
function pushbutton_v5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'on');
set(handles.pixcel_v, 'Enable', 'on');
set(handles.edit_pixcel_v, 'Enable', 'on');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in pushbutton_v7.
function pushbutton_v7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'on');
set(handles.pixcel_v, 'Enable', 'on');
set(handles.edit_pixcel_v, 'Enable', 'on');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_v8.
function pushbutton_v8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'on');
set(handles.pixcel_v, 'Enable', 'on');
set(handles.edit_pixcel_v, 'Enable', 'on');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in checkbox_spacing_v.
function checkbox_spacing_v_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_spacing_v (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_spacing_v
global checkVselected
if get(hObject,'Value')
    % Enable the edit box
    set(handles.edit_pixcel_v, 'Enable', 'on');
    checkVselected = 1;
else
    % Disable the edit box
    set(handles.edit_pixcel_v, 'Enable', 'off');
    checkVselected = 0;
end

% Update handles structure
guidata(hObject, handles);

function edit_pixcel_v_Callback(hObject, eventdata, handles)
% hObject    handle to edit_pixcel_v (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_pixcel_v as text
%        str2double(get(hObject,'String')) returns contents of edit_pixcel_v as a double

global nDistV
nDistV = str2double(get(hObject,'String'));


% --- Executes during object creation, after setting all properties.
function edit_pixcel_v_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_pixcel_v (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_h1.
function pushbutton_h1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'off');
set(handles.pixcel_h, 'Enable', 'off');
set(handles.edit_pixcel_h, 'Enable', 'off');

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_h2.
function pushbutton_h2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'off');
set(handles.pixcel_h, 'Enable', 'off');
set(handles.edit_pixcel_h, 'Enable', 'off');

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_h3.
function pushbutton_h3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'off');
set(handles.pixcel_h, 'Enable', 'off');
set(handles.edit_pixcel_h, 'Enable', 'off');

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_h4.
function pushbutton_h4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'off');
set(handles.pixcel_h, 'Enable', 'off');
set(handles.edit_pixcel_h, 'Enable', 'off');

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in pushbutton_h5.
function pushbutton_h5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'on');
set(handles.pixcel_h, 'Enable', 'on');
set(handles.edit_pixcel_h, 'Enable', 'on');

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in pushbutton_h6.
function pushbutton_h6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'on');
set(handles.pixcel_h, 'Enable', 'on');
set(handles.edit_pixcel_h, 'Enable', 'on');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton_h7.
function pushbutton_h7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'on');
set(handles.pixcel_h, 'Enable', 'on');
set(handles.edit_pixcel_h, 'Enable', 'on');

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in pushbutton_h8.
function pushbutton_h8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_h8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_h)
    set(handles.button_pressed_h, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_h = hObject;

set(handles.checkbox_spacing_h, 'Enable', 'on');
set(handles.pixcel_h, 'Enable', 'on');
set(handles.edit_pixcel_h, 'Enable', 'on');
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in checkbox_spacing_h.
function checkbox_spacing_h_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_spacing_h (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_spacing_h
global checkHselected
if get(hObject,'Value')
    % Enable the edit box
    set(handles.edit_pixcel_h, 'Enable', 'on');
    checkHselected = 1;
else
    % Disable the edit box
    set(handles.edit_pixcel_h, 'Enable', 'off');
    checkHselected = 0;
end

% Update handles structure
guidata(hObject, handles);

function edit_pixcel_h_Callback(hObject, eventdata, handles)
% hObject    handle to edit_pixcel_h (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_pixcel_h as text
%        str2double(get(hObject,'String')) returns contents of edit_pixcel_h as a double


% --- Executes during object creation, after setting all properties.
function edit_pixcel_h_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_pixcel_h (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_ok.
function pushbutton_ok_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_ok (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

bStatus = FcnMasterAlignBlock(handles);
if bStatus == 1
    close(gcf);
    close;
end


% --- Executes on button press in pushbutton_apply.
function pushbutton_apply_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_apply (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

FcnMasterAlignBlock(handles);

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in pushbutton_cancel.
function pushbutton_cancel_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_cancel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

close(gcf);


% --- Executes on button press in pushbutton_v6.
function pushbutton_v6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_v6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if ~isempty(handles.button_pressed_v)
    set(handles.button_pressed_v, 'Enable', 'on');
end
% disable the pushbutton for OFF on press
set(hObject, 'Enable', 'off');

% update the handle for pressed button
handles.button_pressed_v = hObject;

set(handles.checkbox_spacing_v, 'Enable', 'on');
set(handles.pixcel_v, 'Enable', 'on');
set(handles.edit_pixcel_v, 'Enable', 'on');
% Update handles structure
guidata(hObject, handles);



% --- Executes on selection change in popupmenu_add.
function popupmenu_add_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_add (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu_add contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_add


% --- Executes during object creation, after setting all properties.
function popupmenu_add_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_add (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function selected_blocks = get_selected_blocks


selected_blocks = find_system(get_param(gcs, 'Handle'), 'LookUnderMasks', 'all', ...
    'FollowLinks', 'on', 'SearchDepth', 1, 'Type', 'block', 'Selected', 'on');

if isempty(selected_blocks)
    warndlg('No blocks are selected for alignment');
    
    return;
end

% get the parent

strParent = get_param(selected_blocks, 'Parent');
if strcmp(gcs, strParent)
    return;
end

uniqueparent = unique(strParent);

if length(uniqueparent) == 1
    % donothing
else
    tempLen = length(uniqueparent{1});
    tempName = uniqueparent{1};

    for iCount = 2:length(uniqueparent)
        if tempLen > length(uniqueparent{iCount})
            tempLen  = length(uniqueparent{iCount});
            tempName = uniqueparent{iCount};
        end
    end

    toremove =find( strcmpi(tempName, strParent)== 1);
    selected_blocks(toremove) = '';
end

function alignBlock(handles, selected_blocks)

% get the enable status of vertical and hoprizontal pane
verOffStatus = get(handles.pushbutton_v1, 'Enable');
horOffStatus = get(handles.pushbutton_h1, 'Enable');

if strcmp(verOffStatus, 'on')
    buttonSelected = get(handles.button_pressed_v, 'Tag');
    feval(['Fcn', buttonSelected], selected_blocks);
end

if strcmp(horOffStatus, 'on')
    buttonSelected = get(handles.button_pressed_h, 'Tag');
    feval(['Fcn', buttonSelected], selected_blocks);
end


function Fcnpushbutton_v2(selected_blocks)

% get the position of the blocks
position = get_param(selected_blocks, 'Position');
[a b] = size(position);

if  a == 1
    position = {position};
end

% detect the top placed block
top = [];
bottom = [];
for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    top = [top currblk(2)];
    bottom = [bottom currblk(4)];
end

%respective block height
blkheight = top - bottom;

% top height
top = min(top);

for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    blockPos = [currblk(1) top currblk(3) (top - blkheight(iCount))];
    set_param(selected_blocks(iCount), 'Position', blockPos);
end
    

function Fcnpushbutton_v3(selected_blocks)

% get the position of the blocks
position = get_param(selected_blocks, 'Position');
[a b] = size(position);

if  a == 1
    position = {position};
end


% detect the top placed block
top = [];
bottom = [];
for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    top = [top currblk(2)];
    bottom = [bottom currblk(4)];
end

%respective block height
blkheight = top - bottom;

% top height
top = (max(top) + min(top))/2;

for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    blockPos = [currblk(1) top currblk(3) (top - blkheight(iCount))];
    set_param(selected_blocks(iCount), 'Position', blockPos);
end



function Fcnpushbutton_v4(selected_blocks)
% get the position of the blocks
position = get_param(selected_blocks, 'Position');
[a b] = size(position);

if  a == 1
    position = {position};
end


% detect the top placed block
top = [];
bottom = [];
for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    top = [top currblk(2)];
    bottom = [bottom currblk(4)];
end

%respective block height
blkheight = top - bottom;

% top height
top = max(top);

for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    blockPos = [currblk(1) top currblk(3) (top - blkheight(iCount))];
    set_param(selected_blocks(iCount), 'Position', blockPos);
end


function Fcnpushbutton_v5(selected_blocks)

global checkVselected
if checkVselected == 0
    warndlg('Set spacing');
    return;
end

% sort the block with position
BlockHandles = sortblock(selected_blocks);

% get the distance required
handleArray = BlockHandles;
global nDistV
ndist = nDistV;
pos = get_param(handleArray(1), 'Position');
botStore = pos(4);

for iCount = 2:length(handleArray)
    currPos = get_param(handleArray(iCount), 'Position');
    height = currPos(4) - currPos(2);
    currPos(2) = botStore + ndist;
    currPos(4) = currPos(2) + height;
    botStore = currPos(4);
    set_param(handleArray(iCount), 'Position', currPos);
end




function Fcnpushbutton_v6(selected_blocks)

global checkVselected
if checkVselected == 0
    warndlg('Set spacing');
    return;
end
% sort the block with position
BlockHandles = sortblock(selected_blocks);

% get the distance required
handleArray = BlockHandles;
global nDistV
ndist = nDistV;
pos = get_param(handleArray(1), 'Position');
botStore = pos(2);

for iCount = 2:length(handleArray)
    currPos = get_param(handleArray(iCount), 'Position');
    height = currPos(4) - currPos(2);
    currPos(2) = botStore + ndist;
    currPos(4) = currPos(2) + height;
    botStore = currPos(2);
    set_param(handleArray(iCount), 'Position', currPos);
end

function Fcnpushbutton_v7(selected_blocks)

global checkVselected
if checkVselected == 0
    warndlg('Set spacing');
    return;
end

Fcnpushbutton_v5(selected_blocks)

function Fcnpushbutton_v8(selected_blocks)

global checkVselected
if checkVselected == 0
    warndlg('Set spacing');
    return;
end

% sort the block with position
BlockHandles = sortblock(selected_blocks);

% get the distance required
handleArray = BlockHandles;
global nDistV
ndist = nDistV;
pos = get_param(handleArray(1), 'Position');
% get the bottom position
botStore = pos(4);

for iCount = 2:length(handleArray)
    currPos = get_param(handleArray(iCount), 'Position');
    height = currPos(4) - currPos(2);
    currPos(4) = botStore + ndist;
    currPos(2) = currPos(4) - height;
    botStore = currPos(4);
    set_param(handleArray(iCount), 'Position', currPos);
end

function Fcnpushbutton_h2(selected_blocks)

% get the position of the blocks
position = get_param(selected_blocks, 'Position');
[a b] = size(position);

if  a == 1
    position = {position};
end


% detect the left placed block
left = [];
right = [];
for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    left = [left currblk(1)];
    right = [right currblk(3)];
end

%respective block weidth
blkweigth = right - left;

% left height
left = min(left);

for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    blockPos = [left currblk(2) (left + blkweigth(iCount)) currblk(4)];
    set_param(selected_blocks(iCount), 'Position', blockPos);
end


function Fcnpushbutton_h3(selected_blocks)

% get the position of the blocks
position = get_param(selected_blocks, 'Position');
[a b] = size(position);

if  a == 1
    position = {position};
end


% detect the left placed block
left = [];
right = [];
for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    left = [left currblk(1)];
    right = [right currblk(3)];
end

% do nothing if all the blocks are having same left intend
if (length(unique(left)) == 1)
    return;
end

%respective block weidth
blkweigth = right - left;

% centre point
leftPoint = (min(left) + max(right))/2;

for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    blockPos = [leftPoint currblk(2) (leftPoint + blkweigth(iCount)) currblk(4)];
    set_param(selected_blocks(iCount), 'Position', blockPos);
end

function Fcnpushbutton_h4(selected_blocks)

% get the position of the blocks
position = get_param(selected_blocks, 'Position');
[a b] = size(position);

if  a == 1
    position = {position};
end


% detect the left placed block
left = [];
right = [];
for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    left = [left currblk(1)];
    right = [right currblk(3)];
end

%respective block weidth
blkweigth = right - left;

% max right point
right = max(right);

for iCount = 1:length(position)
    currblk = cell2mat(position(iCount));
    blockPos = [(right - blkweigth(iCount)) currblk(2) right currblk(4)];
    set_param(selected_blocks(iCount), 'Position', blockPos);
end

function Fcnpushbutton_h5(selected_blocks)

global checkHselected
if checkHselected == 0
    warndlg('Set spacing');
    return;
end

% sort the block with position
BlockHandles = sortblock(selected_blocks);

% get the distance required
handleArray = BlockHandles;
global nDistV
ndist = nDistV;
pos = get_param(handleArray(1), 'Position');
botStore = pos(3);

for iCount = 2:length(handleArray)
    currPos = get_param(handleArray(iCount), 'Position');
    width = currPos(3) - currPos(1);
    currPos(1) = botStore + ndist;
    currPos(3) = currPos(1) + width;
    botStore = currPos(3);
    set_param(handleArray(iCount), 'Position', currPos);
end

function Fcnpushbutton_h6(selected_blocks)

global checkHselected
if checkHselected == 0
    warndlg('Set spacing');
    return;
end

% sort the block with position
BlockHandles = sortblock(selected_blocks);

% get the distance required
handleArray = BlockHandles;

global nDistV
ndist = nDistV;
pos = get_param(handleArray(1), 'Position');
botStore = pos(1);

for iCount = 2:length(handleArray)
    currPos = get_param(handleArray(iCount), 'Position');
    width = currPos(3) - currPos(1);
    currPos(1) = botStore + ndist;
    currPos(3) = currPos(1) + width;
    botStore = currPos(1);
    set_param(handleArray(iCount), 'Position', currPos);
end

function Fcnpushbutton_h7(selected_blocks)

global checkHselected
if checkHselected == 0
    warndlg('Set spacing');
    return;
end

Fcnpushbutton_h5(selected_blocks);

function Fcnpushbutton_h8(selected_blocks)

global checkHselected
if checkHselected == 0
    warndlg('Set spacing');
    return;
end

% sort the block with position
BlockHandles = sortblock(selected_blocks);

% get the distance required
handleArray = BlockHandles;
global nDistV
ndist = nDistV;
pos = get_param(handleArray(1), 'Position');
botStore = pos(3);

for iCount = 2:length(handleArray)
    currPos = get_param(handleArray(iCount), 'Position');
    width = currPos(3) - currPos(1);
    currPos(3) = botStore + ndist;
    currPos(1) = currPos(3) - width;
    botStore = currPos(3);
    set_param(handleArray(iCount), 'Position', currPos);
end

function bStatus = FcnMasterAlignBlock(handles)
bStatus = 1;
selected_blocks  = get_selected_blocks;

if isempty(selected_blocks)
    bStatus = 0;
    return;
end

alignBlock(handles, selected_blocks)


function BlockHandles = sortblock(selected_blocks)

% detect the top placed block
top = [];
for iCount = 1:length(selected_blocks)
    currblk = get_param(selected_blocks(iCount), 'Position');
    top = [top currblk(2)];
end

top = sort(top);
BlockHandles = zeros(1, length(top));
for iCount = 1:length(selected_blocks)
    currblk = get_param(selected_blocks(iCount), 'Position');
    toppos = currblk(2);
    index = find(top == toppos);
    if length(index)== 1
        BlockHandles(index)= selected_blocks(iCount);
    else
        for i = 1:length(index)
            if BlockHandles(i) == 0
                BlockHandles(index(i)) = selected_blocks(iCount);
                break;
            end
        end
    end
end

Contact us at files@mathworks.com