www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@conbase/mv2sl.m
function mv2sl(con, mdlFullPath) %MV2SL Build a Simulink version of a constraint. % % [OK, MSG] = MV2SL(CON, MDLFULLPATH) % Copyright 2005-2012 The MathWorks, Inc. NF = nFactors( con ); % If the user hasn't supplied a file name then use a temporary one if nargin < 2, [~, mdlFullPath] = fileparts(tempname); end [mdlPath, mdlName,ext] = fileparts( mdlFullPath ); if isempty( mdlPath ), mdlPath = pwd; end if isempty(ext) mdlFullPath = fullfile( mdlPath,[mdlName, '.', get_param(0,'ModelFileFormat')]); end currentDir = cd( mdlPath ); % Check the name of the model and create a new system if it doesn't % already exist if ~exist( mdlFullPath, 'file' ), new_system( mdlName ); end open_system( mdlName ); set_param( mdlName, 'ModelBrowserVisibility', 'on' ); % Add a subsystem to hold the constraint hSubsystem = i_AddBlock( 'built-in/SubSystem', mdlName, getName( con ) ); set_param( hSubsystem, 'position', [10, 10, 200, 20+30*NF] ); % Add inport blocks to the subsystem cif = getInputFactors( con ); hInports = zeros( NF, 1 ); pos = [30, 0, 50, 20] ; % LTRB names = getFullNames( cif ); for i = 1:NF, hInports(i) = i_AddBlock( 'built-in/Inport', hSubsystem, names{i} ); pos = pos + [0, 40, 0, 40]; set_param( hInports(i), 'position', pos ); end % Add a mux block to the subsystem hMux = i_AddBlock( 'built-in/mux', hSubsystem, 'Mx1' ); set_param( hMux, 'position', [90, 40, 95, 20+40*NF], ... 'inputs', num2str( NF ), ... 'displayoption', 'bar', ... 'ShowName', 'off' ); % Add the constraint block to the subsystem [junk, hConstraint] = slblock( con, getfullname( hSubsystem ) ); set_param( hConstraint, 'position', [120, 40, 280, 20+40*NF] ); % Add outport blocks to the subsystem hOutportInOut = i_AddBlock( 'built-in/Outport', hSubsystem, 'InOut' ); hOutportPoint = i_AddBlock( 'built-in/Outport', hSubsystem, 'Interior Point' ); set_param( hOutportInOut, 'position', [320, 40, 340, 60] ); set_param( hOutportPoint, 'position', [320, 40*NF, 340, 20+40*NF] ); % Wire up all the blocks in the system for i = 1:NF, i_AddLine( hInports(i), 1, hMux, i ); end i_AddLine( hMux, 1, hConstraint, 1 ); i_AddLine( hConstraint, 1, hOutportInOut, 1 ); i_AddLine( hConstraint, 2, hOutportPoint, 1 ); % Save system save_system(mdlName, mdlFullPath ); cd( currentDir ); %-------------------------------------------------------------------------- function hBlock = i_AddBlock( srcBlock, tgtSystem, blockName, varargin ) % Find and remove any slashes ('/') in the new block name slashFreeName = strrep( blockName, '/', ' ' ); tgt = sprintf( '%s/%s', getfullname( tgtSystem ), slashFreeName ); hBlock = add_block( srcBlock, tgt, ... 'MakeNameUnique', 'On', ... 'LinkStatus', 'None', ... varargin{:} ); % If there were slashes in the given name then we try to reassign the block % with the desired name. if ~strcmp(slashFreeName, blockName), try set_param( hBlock, 'Name', blockName ); catch % The most likely cause of a slash in a block name is if the user % has used a slash in the variable name, symbol or units. These % informations are used to generate the input (inport) names for % the constraint block. If we fail with the renaming, the most % likely cause is that the user has more than one variable with the % same name, symbol and units. In this case, they will have to wear % the fact that their inport names are slightly odd (but only % slightly). end end %-------------------------------------------------------------------------- function i_AddLine( srcBlock, srcPort, tgtBlock, tgtPort ) % We assume that the srcBlock and tgtBlock belong to the same (sub) system. % If they don't then there will no doubt be an error. system = get_param( srcBlock, 'Parent' ); % Use the port handles as there may be funny characters in the names and % it's better to avoid them hSrcPorts = get_param( srcBlock, 'PortHandles' ); hTgtPorts = get_param( tgtBlock, 'PortHandles' ); % Add the line add_line( system, ... hSrcPorts.Outport(srcPort), ... hTgtPorts.Inport(tgtPort), ... 'autorouting', 'on' ); %-------------------------------------------------------------------------- % EOF %--------------------------------------------------------------------------