No BSD License  

Highlights from
MATLAB/Simulink Unit Test Framework

from MATLAB/Simulink Unit Test Framework by Carl Kritzinger
A unit test framework for matlab and simulink components

make_general_adder(sys,n_inputs)
%another arbitrary file included for demo purposes.
%make a subsystem that adds all its inputs together.

%Each atomic adder element can only handle a maximum of 2 inputs.  This
%function will create an adder to add any number of inputs together.
function make_general_adder(sys,n_inputs)

global adder_block;
%adder_block = 'syndsplibv4/Math Functions/Add';
adder_block = 'built-in/Sum';

max_inputs_per_adder_block = 2;
global adder_no;
adder_no = 1;

%clear the system
clear_system(sys);

draw_params = setup_default_draw_parameters();

% set up the N inputs (real and complex)
%----------------------------------------
[to_be_added,draw_params] = add_column_of_blocks(sys,n_inputs,'built-in/Inport','input',draw_params);
        draw_params = increment_h_pos(draw_params);

iterations_in_this_column = floor(length(to_be_added)/max_inputs_per_adder_block);

while length(to_be_added) >= max_inputs_per_adder_block + 1

    to_be_added = new_adder(sys,to_be_added,max_inputs_per_adder_block,draw_params);
    
    if(iterations_in_this_column>1)
        iterations_in_this_column = iterations_in_this_column-1;
        draw_params = increment_v_pos(draw_params);

    else
        iterations_in_this_column = floor(length(to_be_added)/max_inputs_per_adder_block);
        draw_params = increment_h_pos(draw_params);
    end

end

to_be_added = new_adder(sys,to_be_added,length(to_be_added),draw_params);
draw_params = increment_h_pos(draw_params);
outport = add_simulink_block(sys,'built-in/Outport','sum',draw_params);
link_blocks(sys,to_be_added{1},1,outport,1);


function to_be_added = new_adder(sys,to_be_added,n_inputs,draw_params)
global adder_no;
global adder_block;

%insert the new adder block
[adder] = add_simulink_block(sys,adder_block,['adder_' num2str(adder_no)],draw_params);

%set up the adder block
set_adder_n_inputs(adder,n_inputs)

%connect it to the top N blocks in to_be_added
for kk = 1:n_inputs
    link_blocks(sys,to_be_added{kk},1,adder,kk);
end

%remove the top N items and add the adder block's handle to the bottom of the stack
to_be_added = {to_be_added{n_inputs+1:end},adder};

adder_no = adder_no+1;
function set_adder_n_inputs(adder,n_inputs)

y = get_param(adder,'MaskValues');
y{1} = strrep(blanks(n_inputs),' ','+');
set_param(adder,'MaskValues',y);

Contact us at files@mathworks.com