📄 nswitch.m
字号:
function nswitch(n);
%--------------------------------------------------------------------------
% NSWITCH is a routine to generate an n-switch block for graphical Simulink
% systems. This is a switch with n+1 inputs, of which the first input is a
% control signal for the switch which specifies which of the other inputs
% may pass through. The output of the n-switch is equal to:
%
% y = u(u[1]+1)
%
% where u[1] is the switch-control signal.
%
% NSWITCH(N) starts up the automatic block-construction of an n-switch
% with N inputs (excluding the additional control input). If the input
% argument N is not specified, the routine will automatically ask for the
% number of inputs.
%
% The block generated by NSWITCH is a masked Simulink subsystem. Double-
% click this block to see what kind of structure is actually generated
% (and be surprised!).
%--------------------------------------------------------------------------
% If n is not specified, ask for user-input
% -----------------------------------------
if nargin == 0
clc;
n = input('Number of inputsignals: ');
end;
disp(['Starting ',num2str(n),'-switch generation']);
% The generated n-switch is stored in a new graphical system 'n-switch'
% which will automatically be shown on screen.
% ---------------------------------------------------------------------
sys = ['gen ',num2str(n),'-switch'];
new_system(sys)
simver(1.2)
set_param(sys,'Location',[20,10,150,min(90+n*10,350)])
open_system(sys)
% Subsystem n-switch, where n is the value, specified as additional
% S-function input.
% -----------------------------------------------------------------
blname = [num2str(n),'-switch'];
new_system([sys,'/',blname])
set_param([sys,'/',blname],'Location',[0,0,450,400])
% Loop to build the blocks for the n different inputsignals
% ---------------------------------------------------------
for i = 1:n
% Add Inport block for i'th signal
% --------------------------------
nr = num2str(i+1);
name = [blname,'/u',num2str(i)];
x = 60 + i*50; % 110 + (i-1)*50
y = 80 + i*50; % 130 + (i-1)*50
add_block('built-in/Inport',[sys,'/',name])
set_param([sys,'/',name],'Drop Shadow',4,'Port',nr,...
'position',[195,x,215,y])
% Add Product block for i'th signal
% ---------------------------------
nr = num2str(i);
name = [blname,'/prod',nr];
x = 45 + i*50; % 95 + (i-1)*50
y = 65 + i*50; % 115 + (i-1)*50
add_block('built-in/Product',[sys,'/',name])
set_param([sys,'/',name],'hide name',0,'position',[250,x,270,y])
% Add Function block for i'th signal (criterion that decides if input
% is sent through)
% -------------------------------------------------------------------
name = [blname,'/',nr]; % nr still equal to num2str(i)
x = 40 + i*50; % 90 + (i-1)*50
y = 60 + i*50; % 110 + (i-1)*50
add_block('built-in/Fcn',[sys,'/',name])
set_param([sys,'/',name],'ForeGround',2,'hide name',0,...
'Expr',['u[1]==',nr],'position',[95,x,150,y])
% Create or extend vector of plusses for the Sum block
% ----------------------------------------------------
plusses = [plusses,'+'];
end
% Position parameters for Sum block and Outport block
% ---------------------------------------------------
p = 84;
q = 76 + n*50;
% Add Sum block
% -------------
name = [blname,'/Sum'];
add_block('built-in/Sum',[sys,'/',name])
set_param([sys,'/',name],'hide name',0,'inputs',plusses,...
'position',[340,p,355,q])
% Add Inport block for the switch-signal itself
% ---------------------------------------------
name = [blname,'/Which',13,'input?'];
add_block('built-in/Inport',[sys,'/',name])
set_param([sys,'/',name],'ForeGround',2,'Drop Shadow',4,...
'position',[30,90,50,110])
% Add Outport block at vertical position of output of Sum block
% -------------------------------------------------------------
name = [blname,'/y'];
add_block('built-in/Outport',[sys,'/',name])
set_param([sys,'/',name],'Drop Shadow',4,...
'position',[390,(p+q)/2-10,410,(p+q)/2+10])
% Add line from switch-signal Inport
% ----------------------------------
add_line([sys,'/',blname],[55,100;75,100;75,50+n*50])
%add_line([sys,'/',blname],[75,100;75,50+n*50]) %;85,50+n*50])
% Loop to build the lines for the n different input signals
% ---------------------------------------------------------
for i = 1:n
add_line([sys,'/',blname],[75,50+i*50;85,50+i*50])
add_line([sys,'/',blname],[220,70+i*50;240,60+i*50])
add_line([sys,'/',blname],[275,55+i*50;330,55+i*50])
add_line([sys,'/',blname],[155,50+i*50;240,50+i*50])
end
% Add line to Outport block
% -------------------------
add_line([sys,'/',blname],[360,(p+q)/2;380,(p+q)/2])
% Define Mask Display for the n-switch block we've just created
% -------------------------------------------------------------
set_param([sys,'/',blname],'Mask Display',[num2str(n),' switch:\ny=u(u[1]+1)'])
% Finished composite block 'n-switch'; define block-parameters
% (block-size and position)
% ------------------------------------------------------------
set_param([sys,'/',blname],'position',[30,30,100,max(60,38+10*n)])
%------------------------------------------------------------------------------
% The FDC toolbox. Copyright Marc Rauw, 1994-2000.
% Last revision of this program: October 7, 1997.
%
% Revision history since October 7, 1997:
% =======================================
% October 7, 1997
% - Editorial changes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -