No BSD License  

Highlights from
simbus

from simbus by Sanjai Singh
The DataBus Blockset provides a method for manipulating signals from a vector in SIMULINK.

setnin(sys,BusSize,VecSizes,ShowBusFlag)
function setnin(sys,BusSize,VecSizes,ShowBusFlag)
%SETNIN Sets the number of inports connected to a block in a subsystem.
%
%For use only with the SIMULINK Bus connection blocks.
%
%See also SETNOUT

%Copyright (c) 1995 by The MathWorks, Inc.
%$Revision: 1.1 $ $Date: 1997/02/11 20:51:39 $
Version = version;
v5 = strcmp(Version(1),'5');

if (nargin > 2) 
    Size = length(BusSize) + length(VecSizes);
else
    Size = length(BusSize);
end  

if (Size==1)
    NumInputs = BusSize;
else
    NumInputs = Size;
end

% Error checking.
if (NumInputs<=0)
    error('Must pass SETNIN 1 or more inputs.');
end

% What block to look for
BlockName = 'Mux';

% Determine whether the number of Inports should be increased or decreased.

Blocks = get_param(sys,'blocks');
NumBlocks = size(Blocks,1);

if v5
  eval('Blocks = strvcat(Blocks{:});')
end
% Find the number of Inports
NumInports = 0;
for i=1:NumBlocks
    NumInports = NumInports + any(findstr(Blocks(i,:),'I'));
end

% Setup the default horizontal offset
dx=20;
% Setup the default vertical offset
dy=40;

if (nargin > 3)
    % If showbusflag is 1, the bus port should show at the top level
    if (ShowBusFlag)
        % Determine if port I1 is an inport or a constant
        if (strcmp(get_param([sys '/I1'],'BlockType'), 'Constant'))
            % Port I1 is a constant block, delete it and the corresponding line.
            delete_line(sys,'I1/1',[BlockName '/1']);
            delete_block([sys '/I1']);
            % Now add the input port
            add_block('built-in/Inport',[sys '/I1']);
	    if v5
	      set_param([sys '/I1'],'Port','1','position',[dx,dy,dx+20,dy+20]);
	    else	      
	      set_param([sys '/I1'],'Port',1,'position',[dx,dy,dx+20,dy+20]);
	    end
            add_line(sys,'I1/1',[BlockName '/1']);
            % Renumber the remaining input ports
            for (i=2:NumInports)
	      if v5
		set_param([sys '/I' num2str(i)],'Port',num2str(i));
	      else                
		set_param([sys '/I' num2str(i)],'Port',i);
	      end
            end
        end
    else
        % Determine if port I1 is an inport or a constant
        if (strcmp(get_param([sys '/I1'],'BlockType'), 'Inport'))
            % Port I1 is an inport block, delete it and the corresponding line.
            delete_line(sys,'I1/1',[BlockName '/1']);
            delete_block([sys '/I1']);
            % Now add the constant block
            add_block('built-in/Constant',[sys '/I1']);
            set_param([sys '/I1'],'position',[dx,dy,dx+20,dy+20]);
            add_line(sys,'I1/1',[BlockName '/1']);
            % Renumber the remaining input ports
            for (i=2:NumInports)
	      if v5                
		set_param([sys '/I' num2str(i)],'Port',num2str(i-1));
	      else
                set_param([sys '/I' num2str(i)],'Port',i-1);
	      end
            end
        end
        % Set the size of the constant
	if v5        
	  set_param([sys '/I1'],'Value',mat2str(zeros(BusSize,1)));
	else
	  set_param([sys '/I1'],'Value',zeros(BusSize,1));
	end
    end
end  

% If Inports are to be added...
if (NumInputs>NumInports)
    % Set the number of inputs
    if (nargin > 2)
        VecSizes=strrep(strrep(mat2str(VecSizes),'[',''),']','');
        set_param([sys '/' BlockName],'inputs',['[' num2str(BusSize) ' ' VecSizes ']']);
    else
        set_param([sys '/' BlockName],'inputs',num2str(BusSize));
    end    
    
    % Add the blocks and lines.
    for i=(NumInports+1):NumInputs
        name = ['I' num2str(i)];
        add_block('built-in/Inport',[sys '/' name]);
        port=str2num(get_param([sys '/I' num2str(i-1)],'Port')) + 1;
	if v5        
	  set_param([sys '/' name],'Port',num2str(port),'position',[dx,i*dy,dx+20,i*dy+20]);
	else
	  set_param([sys '/' name],'Port',port,'position',[dx,i*dy,dx+20,i*dy+20]);
	end
	add_line(sys,[name '/1'],[BlockName '/' num2str(i)]);
    end
    return
end  

% If Inports are to be deleted...
if (NumInputs<NumInports)
    % Delete the lines and blocks.
    for i=NumInports:-1:(NumInputs+1)
        name = ['I' num2str(i)];
        delete_line(sys,[name '/1'],[BlockName '/' num2str(i)]);
        delete_block([sys '/' name]);
    end
end

% Set the number of inputs
if (nargin > 2)
    VecSizes=strrep(strrep(mat2str(VecSizes),'[',''),']','');
    set_param([sys '/' BlockName],'inputs',['[' num2str(BusSize) ' ' VecSizes ']']);
else  
    set_param([sys '/' BlockName],'inputs',num2str(BusSize));
end    

% End.





Contact us at files@mathworks.com