⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bdalign.m

📁 数字通信第四版原书的例程
💻 M
字号:
function bdalign(inst)
%BDALIGN Align selected blocks in current SIMULINK window.
%       BDALIGN align the selected blocks in the current SIMULINK window to begin with
%   the same horizontal position.
%        BDALIGN(INST) align the selected blocks in the current SIMULINK window
%   following the instruction in INST. INST is a string variable. INST
%   one of the following string.
%   'vert': Vertical alignment. Make the selected blocks begin at the same
%           horizontal position as the selected most top block.
%           'vert/b': same horizontal position as the selected most bottom block
%   'hori': Horizontal alignment. Make the selected blocks begining at the same
%           vertical position as the selected most left block. When INST
%           contains 'vert', this instruction is ignored.
%           'hori/r': same vertical position as the selected most rigth block
%   'heig': Height adjustment. Make the selected blocks having the same height
%           as the most left selected block. 
%           'heig/t': same height as the selected most top block.
%           'heig/l': same height as the selected most left block (default).
%           'heig/b': same height as the selected most bottom block.
%           'heig/r': same height as the selected most right block.
%   'widt': Width adjustment. make the selected blocks having the same width
%           as the selected most left block.
%           'widt/t': same width as the selected most top block.
%           'widt/l': same width as the selected most left block (default).
%           'widt/b': same width as the selected most bottom block.
%           'widt/r': same width as the selected most right block.
%   'v-d':  Vertical distance adjustment. Make the selected block have the 
%           same distance as the distance of the selected most top two blocks.
%           'v-d/b' same vertical distance as the selected two most bottom blocks.
%   'h-d':  Horizontal distance adjustment. Make the selected block have the 
%           same distance as the distance of the selected most left two blocks.
%           'v-d/r' same horizontal distance as the selected two most left blocks.
%   'undo': To return to the original status of the very last bdalign. This can 
%           be done only immediately after previous command.
%
%   Warning: There is no undo for this function.

%        Wes Wang 12/15/94
%        Copyright (c) 1994-96 by The MathWorks, Inc.
%        $Revision: 1.1 $  $Date: 1996/04/01 19:31:43 $
%

global align_simulink_win align_bloc align_loca

if nargin < 1
    inst = 'vert';
end;
inst = lower(inst);
if strcmp(inst, 'undo')
    if ~exist(align_simulink_win)
        disp('BDALIGN UNDO can be used only right after BDALIGN is used.');
        return;
    elseif ~strcmp(align_simulink_win, get_param)
        disp('BDALIGN UNDO can be used only right after BDALIGN is used.');
        return;
    end;
    for i = 1 :size(align_bloc, 1)
        set_param([align_simulink_win, '/', deblank(align_bloc(i, :))],...
            'position', align_loca(i, :));
    end;
end;

inst = [inst 'add_some_extra'];

align_simulink_win = get_param;
if isempty(align_simulink_win)
   disp('No SIMULINK window has found.');
   return
end;

bloc = get_param(align_simulink_win,'blocks');
[n_block, m_block] = size(bloc);

% find the position of all selected blocks.
pos = [];
to_del = [];
for i=1:n_block
    if get_param([align_simulink_win, '/' deblank(bloc(i, :))], 'selected')
        pos = [pos; get_param([align_simulink_win, '/' deblank(bloc(i, :))], 'position')];
    else
        to_del = [to_del, i];
    end;
end;
bloc(to_del, :) = [];

%save parameters for undo
align_loca = pos;
align_bloc = bloc;

% if only one or none block is selected return.
if size(pos, 1) < 2
    return;
end;

if ~isempty(findstr(inst, 'vert'))
    % vertical alignment
    % sort by the vertical location.
    if ~isempty(findstr(inst, 'vert/b'))
        [s1, s2] = sort(-pos(:, 2));
    else
        [s1, s2] = sort(pos(:, 2));
    end;
    if s1(1) == s1(2)
        s3 = find(s1==s1(1));
        [s4, s5] = sort(pos(s2(s3), 1));
        s2(s3) = s2(s5);
    end;
    pos = pos(s2, :);
    bloc = bloc(s2, :);
    for i = 2 : size(pos, 1)
        pos(i, 3) = pos(1, 1) + pos(i, 3) - pos(i, 1);
        pos(i, 1) = pos(1, 1);
        set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
            'position', pos(i, :));
    end;
elseif ~isempty(findstr(inst, 'hori'))
    % horizontal alignment
    % sort by the horizontal location
    if ~isempty(findstr(inst, 'hori/r'))
        [s1, s2] = sort(-pos(:, 1));
    else
        [s1, s2] = sort(pos(:, 1));
    end;
    if s1(1) == s1(2)
        s3 = find(s1==s1(1));
        [s4, s5] = sort(pos(s2(s3), 2));
        s2(s3) = s2(s5);
    end;
    pos = pos(s2, :);
    bloc = bloc(s2, :);
    for i = 2 : size(pos, 1)
        pos(i, 4) = pos(1, 2) + pos(i, 4) - pos(i, 2);
        pos(i, 2) = pos(1, 2);
        set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
            'position', pos(i, :));
    end;
end;

if ~isempty(findstr(inst, 'heig'))
    % height adjustment.
    if ~isempty(findstr(inst, 'heig/t'))
        [s1, s2] = sort(pos(:, 2));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 1));
            s2(s3) = s2(s5);
        end;
    elseif ~isempty(findstr(inst, 'heig/b'))
        [s1, s2] = sort(-pos(:, 2));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 1));
            s2(s3) = s2(s5);
        end;
    elseif ~isempty(findstr(inst, 'heig/r'))
        [s1, s2] = sort(-pos(:, 1));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 2));
            s2(s3) = s2(s5);
        end;
    else
        [s1, s2] = sort(pos(:, 1));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 2));
            s2(s3) = s2(s5);
        end;
    end;
    pos = pos(s2, :);
    bloc = bloc(s2, :);
    heig = pos(1,4) - pos(1,2);
    for i = 2 : size(pos, 1)
        pos(i, 4) = pos(i, 2) + heig;
        set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
            'position', pos(i, :));
    end;
end;

if ~isempty(findstr(inst, 'widt'))
    % width adjustment.
    if ~isempty(findstr(inst, 'widt/t'))
        [s1, s2] = sort(pos(:, 2));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 1));
            s2(s3) = s2(s5);
        end;
    elseif ~isempty(findstr(inst, 'widt/b'))
        [s1, s2] = sort(-pos(:, 2));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 1));
            s2(s3) = s2(s5);
        end;
    elseif ~isempty(findstr(inst, 'widt/r'))
        [s1, s2] = sort(-pos(:, 1));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 2));
            s2(s3) = s2(s5);
        end;
    else
        [s1, s2] = sort(pos(:, 1));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 2));
            s2(s3) = s2(s5);
        end;
    end;
    pos = pos(s2, :);
    bloc = bloc(s2, :);
    widt = pos(1,3) - pos(1,1);
    for i = 2 : size(pos, 1)
        pos(i, 3) = pos(i, 1) + widt;
        set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
            'position', pos(i, :));
    end;
end;

if ~isempty(findstr(inst, 'v-d'))
    % vertical distance adjustment.
    if ~isempty(findstr(inst, 'v-d/b'))
        [s1, s2] = sort(-pos(:, 2));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 1));
            s2(s3) = s2(s5);
        end;
        pos = pos(s2, :);
        bloc = bloc(s2, :);
        dist = pos(1,2) - pos(2,4);
        for i = 3 : size(pos, 1)
            tmp = pos(i, 4) - pos(i, 2);
            pos(i, 4) = pos(i-1, 2) - dist;
            pos(i, 2) = pos(i, 4) - tmp;
            set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
                'position', pos(i, :));
        end;
    else
        [s1, s2] = sort(pos(:, 2));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 1));
            s2(s3) = s2(s5);
        end;
        pos = pos(s2, :);
        bloc = bloc(s2, :);
        dist = pos(2,2) - pos(1,4);
        for i = 3 : size(pos, 1)
            tmp = pos(i, 4) - pos(i, 2);
            pos(i, 2) = pos(i-1, 4) + dist;
            pos(i, 4) = pos(i, 2) + tmp;
            set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
                'position', pos(i, :));
        end;
    end;
end;

if ~isempty(findstr(inst, 'h-d'))
    % horizontal distance adjustment.
    if ~isempty(findstr(inst, 'h-d/r'))
        [s1, s2] = sort(-pos(:, 1));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 2));
            s2(s3) = s2(s5);
        end;
        pos = pos(s2, :);
        bloc = bloc(s2, :);
        dist = pos(1,1) - pos(2,3);
        for i = 3 : size(pos, 1)
            tmp = pos(i, 3) - pos(i, 1);
            pos(i, 3) = pos(i-1, 1) - dist;
            pos(i, 1) = pos(i, 3) - tmp;
            set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
                'position', pos(i, :));
        end;
    else
        [s1, s2] = sort(pos(:, 1));
        if s1(1) == s1(2)
            s3 = find(s1==s1(1));
            [s4, s5] = sort(pos(s2(s3), 2));
            s2(s3) = s2(s5);
        end;
        pos = pos(s2, :);
        bloc = bloc(s2, :);
        dist = pos(2,1) - pos(1,3);
        for i = 3 : size(pos, 1)
            tmp = pos(i, 3) - pos(i, 1);
            pos(i, 1) = pos(i-1, 3) + dist;
            pos(i, 3) = pos(i, 1) + tmp;
            set_param([align_simulink_win, '/' deblank(bloc(i, :))], ...
                'position', pos(i, :));
        end;
    end;
end;

drawnow

%--end of bdalign--

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -