image thumbnail
from Dealing with array of buses in Simulink® by Vijay Swaminathan
This demo shows a self modifying masked subsystem to split out the correct subbus from the superbus.

my_script.m
% This script will automatically update the Selector block and the Multiport switch with
% the correct number of ports (based on the MainBus structure) and do a
% bunch of DELETE_LINE and ADD_LINE commands to wire them up.


% Author: Vijay Swaminathan
% Copyright 2009, The MathWorks, Inc.


%% Figure out how many meteors are in the MainBus structure
number_of_meteors = size(MainBus.Elements,1);

%% Check to see how many meteor the block was setup prevously to deal with

% If block is already setup to deal with this many meteors, DO NOTHING,
% Else need to update the ports and wire them up correctly

name_of_subsystem = [bdroot,'/Meteor Selector'];
name_of_busselector = ['/Bus',char(10),'Selector'];
blah = get_param([name_of_subsystem,name_of_busselector],'Ports');
number_of_ports_in_busselector_currently = blah(1,2);
if ( number_of_meteors ~= number_of_ports_in_busselector_currently )

    % DELETE first
    for ind = 1:number_of_ports_in_busselector_currently
        starting_port = ['Bus Selector/',num2str(ind)];
        ending_port = ['Multiport Switch/',num2str(ind+1)];
        delete_line(name_of_subsystem,starting_port,ending_port);
    end

    % SET the correct number of output ports in Bus Selector, name the
    % Selected Signals
    Selected_Signals =[];
    % Make up a vector like 'meteor1,meteor2,meteor3'
    for ind = 1:number_of_meteors
        Selected_Signals = [Selected_Signals,',meteor',num2str(ind)];
    end
    Selected_Signals = Selected_Signals(2:end); %deleting the extra comma
    set_param([name_of_subsystem,name_of_busselector],'OutputSignals',Selected_Signals); %Bus Selctor
    set_param( [name_of_subsystem,'/Multiport Switch'], 'Inputs',num2str(number_of_meteors) ); %Multiport Switch


    % ADD lines to connect Bus Selector to Multiport Switch
    for ind = 1:number_of_meteors
        starting_port = ['Bus Selector/',num2str(ind)];
        ending_port = ['Multiport Switch/',num2str(ind+1)];
        add_line(name_of_subsystem,starting_port,ending_port,'autorouting','on');
    end
end
clear choice number_of_meteors name_of_subsystem name_of_busselector blah number_of_ports_in_busselector_currently ind starting_port ending_port Selected_Signals

Contact us at files@mathworks.com